-
-
Notifications
You must be signed in to change notification settings - Fork 497
Expand file tree
/
Copy pathtiny_gltf_v3.c
More file actions
3653 lines (3470 loc) · 156 KB
/
Copy pathtiny_gltf_v3.c
File metadata and controls
3653 lines (3470 loc) · 156 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 TINYGLTF3_SOURCE_INCLUDED_FROM_HEADER
#include "tiny_gltf_v3.h"
#endif
#ifdef TINYGLTF3_NO_STDLIB
#ifndef TINYGLTF_JSON_NO_STDLIB
#define TINYGLTF_JSON_NO_STDLIB
#endif
#endif
#ifndef TINYGLTF_JSON_MALLOC
#define TINYGLTF_JSON_MALLOC(sz) TINYGLTF3_MALLOC(sz)
#endif
#ifndef TINYGLTF_JSON_REALLOC
#define TINYGLTF_JSON_REALLOC(ptr, sz) TINYGLTF3_REALLOC((ptr), (sz))
#endif
#ifndef TINYGLTF_JSON_FREE
#define TINYGLTF_JSON_FREE(ptr) TINYGLTF3_FREE(ptr)
#endif
#define TINYGLTF_JSON_C_IMPLEMENTATION
#include "tinygltf_json_c.h"
#include <inttypes.h>
#ifndef TINYGLTF3_NO_STDLIB
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#endif
static void *tg3__memcpy(void *dst, const void *src, size_t size) {
#ifndef TINYGLTF3_NO_STDLIB
return memcpy(dst, src, size);
#else
unsigned char *d = (unsigned char *)dst;
const unsigned char *s = (const unsigned char *)src;
while (size--) *d++ = *s++;
return dst;
#endif
}
static void *tg3__memset(void *dst, int value, size_t size) {
#ifndef TINYGLTF3_NO_STDLIB
return memset(dst, value, size);
#else
unsigned char *d = (unsigned char *)dst;
while (size--) *d++ = (unsigned char)value;
return dst;
#endif
}
static int tg3__memcmp(const void *a, const void *b, size_t size) {
#ifndef TINYGLTF3_NO_STDLIB
return memcmp(a, b, size);
#else
const unsigned char *pa = (const unsigned char *)a;
const unsigned char *pb = (const unsigned char *)b;
while (size--) {
if (*pa != *pb) return (int)*pa - (int)*pb;
++pa;
++pb;
}
return 0;
#endif
}
static size_t tg3__strlen(const char *s) {
#ifndef TINYGLTF3_NO_STDLIB
return strlen(s);
#else
const char *p = s;
while (*p) ++p;
return (size_t)(p - s);
#endif
}
static void *tg3__memchr(const void *s, int c, size_t size) {
#ifndef TINYGLTF3_NO_STDLIB
return (void *)memchr(s, c, size);
#else
const unsigned char *p = (const unsigned char *)s;
unsigned char ch = (unsigned char)c;
while (size--) {
if (*p == ch) return (void *)p;
++p;
}
return NULL;
#endif
}
static int tg3__isfinite(double v) {
uint64_t bits = 0;
tg3__memcpy(&bits, &v, sizeof(bits));
return (bits & 0x7ff0000000000000ULL) != 0x7ff0000000000000ULL;
}
static double tg3__fabs(double v) {
uint64_t bits = 0;
tg3__memcpy(&bits, &v, sizeof(bits));
bits &= 0x7fffffffffffffffULL;
tg3__memcpy(&v, &bits, sizeof(v));
return v;
}
static int tg3__vsnprintf(char *buf, size_t size, const char *fmt, va_list ap) {
#ifndef TINYGLTF3_NO_STDLIB
return vsnprintf(buf, size, fmt, ap);
#else
size_t i = 0;
(void)ap;
if (size == 0) return 0;
while (fmt[i] && i + 1u < size) {
buf[i] = fmt[i];
++i;
}
buf[i] = '\0';
return (int)i;
#endif
}
#ifndef TINYGLTF3_MEMCPY
#define TINYGLTF3_MEMCPY(dst, src, size) tg3__memcpy((dst), (src), (size))
#endif
#ifndef TINYGLTF3_MEMSET
#define TINYGLTF3_MEMSET(dst, value, size) tg3__memset((dst), (value), (size))
#endif
#ifndef TINYGLTF3_MEMCMP
#define TINYGLTF3_MEMCMP(a, b, size) tg3__memcmp((a), (b), (size))
#endif
#ifndef TINYGLTF3_STRLEN
#define TINYGLTF3_STRLEN(s) tg3__strlen((s))
#endif
#ifndef TINYGLTF3_MEMCHR
#define TINYGLTF3_MEMCHR(s, c, size) tg3__memchr((s), (c), (size))
#endif
#define TG3__ARENA_DEFAULT_BLOCK_SIZE (256u * 1024u)
#define TG3__ARENA_ALIGNMENT 8u
typedef struct tg3__arena_block {
struct tg3__arena_block *next;
uint8_t *base;
size_t used;
size_t capacity;
} tg3__arena_block;
typedef struct tg3_arena tg3_arena;
struct tg3_arena {
tg3__arena_block *head;
tg3__arena_block *current;
size_t total_allocated;
size_t memory_budget;
size_t max_single_alloc;
size_t block_size;
tg3_allocator alloc;
};
typedef struct tg3__parse_ctx {
tg3_arena *arena;
tg3_error_stack *errors;
tg3_parse_options opts;
const char *base_dir;
uint32_t base_dir_len;
const uint8_t *bin_data;
uint64_t bin_size;
int32_t is_binary;
} tg3__parse_ctx;
struct tg3_writer {
tg3_write_chunk_fn chunk_fn;
void *user_data;
tg3_write_options options;
tg3json_value root;
int begun;
};
static void *tg3__default_alloc(size_t size, void *ud) {
(void)size;
(void)ud;
return TINYGLTF3_MALLOC(size);
}
static void *tg3__default_realloc(void *ptr, size_t old_size, size_t new_size, void *ud) {
(void)ptr;
(void)old_size;
(void)new_size;
(void)ud;
return TINYGLTF3_REALLOC(ptr, new_size);
}
static void tg3__default_free(void *ptr, size_t size, void *ud) {
(void)size;
(void)ud;
TINYGLTF3_FREE(ptr);
}
static tg3_arena *tg3__arena_create(const tg3_memory_config *config) {
tg3_allocator alloc;
tg3_arena *arena;
if (config && config->allocator.alloc) {
alloc = config->allocator;
} else {
alloc.alloc = tg3__default_alloc;
alloc.realloc = tg3__default_realloc;
alloc.free = tg3__default_free;
alloc.user_data = NULL;
}
arena = (tg3_arena *)alloc.alloc(sizeof(tg3_arena), alloc.user_data);
if (!arena) return NULL;
TINYGLTF3_MEMSET(arena, 0, sizeof(*arena));
arena->alloc = alloc;
arena->block_size = (config && config->arena_block_size > 0)
? (size_t)config->arena_block_size
: (size_t)TG3__ARENA_DEFAULT_BLOCK_SIZE;
arena->memory_budget = (config && config->memory_budget > 0)
? (size_t)config->memory_budget
: (size_t)TINYGLTF3_MAX_MEMORY_BYTES;
arena->max_single_alloc = (config && config->max_single_alloc > 0)
? (size_t)config->max_single_alloc
: 0;
return arena;
}
static tg3__arena_block *tg3__arena_new_block(tg3_arena *arena, size_t min_size) {
size_t cap = arena->block_size;
void *raw;
tg3__arena_block *block;
if (cap < min_size) cap = min_size;
if (arena->max_single_alloc && cap > arena->max_single_alloc) return NULL;
if (arena->total_allocated + sizeof(tg3__arena_block) + cap > arena->memory_budget) return NULL;
raw = arena->alloc.alloc(sizeof(tg3__arena_block) + cap, arena->alloc.user_data);
if (!raw) return NULL;
block = (tg3__arena_block *)raw;
block->next = NULL;
block->base = (uint8_t *)raw + sizeof(tg3__arena_block);
block->used = 0;
block->capacity = cap;
arena->total_allocated += sizeof(tg3__arena_block) + cap;
if (arena->current) arena->current->next = block;
else arena->head = block;
arena->current = block;
return block;
}
static void *tg3__arena_alloc(tg3_arena *arena, size_t size) {
tg3__arena_block *block;
void *ptr;
if (!arena || size == 0) return NULL;
if (arena->max_single_alloc && size > arena->max_single_alloc) return NULL;
size = (size + (TG3__ARENA_ALIGNMENT - 1u)) & ~(size_t)(TG3__ARENA_ALIGNMENT - 1u);
block = arena->current;
if (!block || (block->used + size > block->capacity)) {
block = tg3__arena_new_block(arena, size);
if (!block) return NULL;
}
ptr = block->base + block->used;
block->used += size;
return ptr;
}
static char *tg3__arena_strdup(tg3_arena *arena, const char *s, size_t len) {
char *dst;
if (!s) return NULL;
dst = (char *)tg3__arena_alloc(arena, len + 1);
if (!dst) return NULL;
if (len > 0) TINYGLTF3_MEMCPY(dst, s, len);
dst[len] = '\0';
return dst;
}
static tg3_str tg3__arena_str(tg3_arena *arena, const char *s, uint32_t len) {
tg3_str out;
out.data = tg3__arena_strdup(arena, s, len);
out.len = out.data ? len : 0;
return out;
}
static void tg3__arena_destroy(tg3_arena *arena) {
tg3_allocator alloc;
tg3__arena_block *block;
if (!arena) return;
alloc = arena->alloc;
block = arena->head;
while (block) {
tg3__arena_block *next = block->next;
alloc.free(block, sizeof(tg3__arena_block) + block->capacity, alloc.user_data);
block = next;
}
alloc.free(arena, sizeof(*arena), alloc.user_data);
}
static void tg3__error_push(tg3_error_stack *es, tg3_severity sev,
tg3_error_code code, const char *msg,
const char *json_path, int64_t byte_offset) {
tg3_error_entry *entry;
tg3_error_entry *new_entries;
uint32_t new_cap;
if (!es) return;
if (es->count >= es->capacity) {
new_cap = es->capacity ? es->capacity * 2u : 16u;
new_entries = (tg3_error_entry *)TINYGLTF3_REALLOC(es->entries, new_cap * sizeof(tg3_error_entry));
if (!new_entries) return;
es->entries = new_entries;
es->capacity = new_cap;
}
entry = &es->entries[es->count++];
entry->severity = sev;
entry->code = code;
entry->message = msg;
entry->json_path = json_path;
entry->byte_offset = byte_offset;
if (sev == TG3_SEVERITY_ERROR) es->has_error = 1;
}
static void tg3__error_pushf(tg3_error_stack *es, tg3_arena *arena,
tg3_severity sev, tg3_error_code code,
const char *json_path, const char *fmt, ...) {
char buf[1024];
int n;
va_list ap;
const char *msg = buf;
if (!es) return;
va_start(ap, fmt);
n = tg3__vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (n < 0) n = 0;
if ((size_t)n >= sizeof(buf)) n = (int)(sizeof(buf) - 1u);
if (arena) {
char *dup = tg3__arena_strdup(arena, buf, (size_t)n);
if (dup) msg = dup;
}
tg3__error_push(es, sev, code, msg, json_path, -1);
}
TINYGLTF3_API int32_t tg3_errors_has_error(const tg3_error_stack *es) { return es ? es->has_error : 0; }
TINYGLTF3_API uint32_t tg3_errors_count(const tg3_error_stack *es) { return es ? es->count : 0; }
TINYGLTF3_API const tg3_error_entry *tg3_errors_get(const tg3_error_stack *es, uint32_t index) {
if (!es || index >= es->count) return NULL;
return &es->entries[index];
}
TINYGLTF3_API void tg3_error_stack_init(tg3_error_stack *es) { if (es) TINYGLTF3_MEMSET(es, 0, sizeof(*es)); }
TINYGLTF3_API void tg3_error_stack_free(tg3_error_stack *es) {
if (!es) return;
TINYGLTF3_FREE(es->entries);
TINYGLTF3_MEMSET(es, 0, sizeof(*es));
}
TINYGLTF3_API void tg3_parse_options_init(tg3_parse_options *options) {
if (!options) return;
TINYGLTF3_MEMSET(options, 0, sizeof(*options));
options->required_sections = TG3_REQUIRE_VERSION;
options->strictness = TG3_PERMISSIVE;
options->memory.memory_budget = TINYGLTF3_MAX_MEMORY_BYTES;
options->memory.arena_block_size = TG3__ARENA_DEFAULT_BLOCK_SIZE;
options->max_external_file_size = 0;
options->validate_indices = 1;
}
TINYGLTF3_API void tg3_write_options_init(tg3_write_options *options) {
if (!options) return;
TINYGLTF3_MEMSET(options, 0, sizeof(*options));
options->pretty_print = 1;
options->memory.memory_budget = TINYGLTF3_MAX_MEMORY_BYTES;
options->memory.arena_block_size = TG3__ARENA_DEFAULT_BLOCK_SIZE;
}
TINYGLTF3_API int32_t tg3_component_size(int32_t component_type) {
switch (component_type) {
case TG3_COMPONENT_TYPE_BYTE:
case TG3_COMPONENT_TYPE_UNSIGNED_BYTE: return 1;
case TG3_COMPONENT_TYPE_SHORT:
case TG3_COMPONENT_TYPE_UNSIGNED_SHORT: return 2;
case TG3_COMPONENT_TYPE_INT:
case TG3_COMPONENT_TYPE_UNSIGNED_INT:
case TG3_COMPONENT_TYPE_FLOAT: return 4;
case TG3_COMPONENT_TYPE_DOUBLE: return 8;
default: return -1;
}
}
TINYGLTF3_API int32_t tg3_num_components(int32_t type) {
switch (type) {
case TG3_TYPE_SCALAR: return 1;
case TG3_TYPE_VEC2: return 2;
case TG3_TYPE_VEC3: return 3;
case TG3_TYPE_VEC4: return 4;
case TG3_TYPE_MAT2: return 4;
case TG3_TYPE_MAT3: return 9;
case TG3_TYPE_MAT4: return 16;
default: return -1;
}
}
TINYGLTF3_API int32_t tg3_accessor_byte_stride(const tg3_accessor *accessor, const tg3_buffer_view *bv) {
int32_t comp;
int32_t num;
if (bv && bv->byte_stride > 0) return (int32_t)bv->byte_stride;
comp = tg3_component_size(accessor->component_type);
num = tg3_num_components(accessor->type);
if (comp < 0 || num < 0) return -1;
return comp * num;
}
TINYGLTF3_API int32_t tg3_str_equals(tg3_str a, tg3_str b) {
if (a.len != b.len) return 0;
if (a.len == 0) return 1;
return TINYGLTF3_MEMCMP(a.data, b.data, a.len) == 0 ? 1 : 0;
}
TINYGLTF3_API int32_t tg3_str_equals_cstr(tg3_str a, const char *b) {
uint32_t blen;
if (!b) return a.len == 0 ? 1 : 0;
blen = (uint32_t)TINYGLTF3_STRLEN(b);
if (a.len != blen) return 0;
if (a.len == 0) return 1;
return TINYGLTF3_MEMCMP(a.data, b, a.len) == 0 ? 1 : 0;
}
static int tg3__b64_decode_char(unsigned char c) {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1;
}
static uint8_t *tg3__b64_decode(const char *input, size_t input_len, size_t *out_len, tg3_arena *arena) {
uint8_t *out;
size_t decoded_len;
size_t i;
size_t j = 0;
uint32_t accum = 0;
int bits = 0;
if (input_len == 0) {
*out_len = 0;
return NULL;
}
while (input_len > 0 && input[input_len - 1] == '=') --input_len;
decoded_len = (input_len * 3u) / 4u;
out = (uint8_t *)tg3__arena_alloc(arena, decoded_len + 1u);
if (!out) {
*out_len = 0;
return NULL;
}
for (i = 0; i < input_len; ++i) {
int val = tg3__b64_decode_char((unsigned char)input[i]);
if (val < 0) continue;
accum = (accum << 6) | (uint32_t)val;
bits += 6;
if (bits >= 8) {
bits -= 8;
out[j++] = (uint8_t)((accum >> bits) & 0xFFu);
}
}
*out_len = j;
return out;
}
TINYGLTF3_API int32_t tg3_is_data_uri(const char *uri, uint32_t len) {
return (uri && len >= 5u && TINYGLTF3_MEMCMP(uri, "data:", 5) == 0) ? 1 : 0;
}
typedef struct tg3__data_uri_result {
const char *data_start;
size_t data_len;
char mime_type[64];
} tg3__data_uri_result;
static int tg3__parse_data_uri(const char *uri, uint32_t uri_len, tg3__data_uri_result *result) {
const char *p;
const char *end;
const char *semi;
size_t mime_len;
if (!uri || uri_len < 5u || TINYGLTF3_MEMCMP(uri, "data:", 5) != 0) return 0;
p = uri + 5;
end = uri + uri_len;
semi = p;
while (semi < end && *semi != ';') ++semi;
if (semi >= end) return 0;
mime_len = (size_t)(semi - p);
if (mime_len >= sizeof(result->mime_type)) mime_len = sizeof(result->mime_type) - 1u;
TINYGLTF3_MEMCPY(result->mime_type, p, mime_len);
result->mime_type[mime_len] = '\0';
p = semi + 1;
if ((size_t)(end - p) < 7u || TINYGLTF3_MEMCMP(p, "base64,", 7) != 0) return 0;
p += 7;
result->data_start = p;
result->data_len = (size_t)(end - p);
return 1;
}
static uint8_t *tg3__decode_data_uri(tg3_arena *arena, const char *uri, uint32_t uri_len,
size_t *out_len, char *out_mime, size_t out_mime_cap) {
tg3__data_uri_result dr;
size_t mlen;
if (!tg3__parse_data_uri(uri, uri_len, &dr)) {
*out_len = 0;
return NULL;
}
if (out_mime && out_mime_cap > 0) {
mlen = TINYGLTF3_STRLEN(dr.mime_type);
if (mlen >= out_mime_cap) mlen = out_mime_cap - 1u;
TINYGLTF3_MEMCPY(out_mime, dr.mime_type, mlen);
out_mime[mlen] = '\0';
}
return tg3__b64_decode(dr.data_start, dr.data_len, out_len, arena);
}
#if defined(TINYGLTF3_ENABLE_FS) && !defined(TINYGLTF3_NO_STDLIB)
static int32_t tg3__fs_file_exists(const char *path, uint32_t path_len, void *ud) {
FILE *fp;
(void)path_len; (void)ud;
fp = fopen(path, "rb");
if (!fp) return 0;
fclose(fp);
return 1;
}
static int32_t tg3__fs_read_file(uint8_t **out_data, uint64_t *out_size,
const char *path, uint32_t path_len, void *ud) {
FILE *fp;
long size;
uint8_t *data;
size_t nread;
(void)path_len; (void)ud;
*out_data = NULL;
*out_size = 0;
fp = fopen(path, "rb");
if (!fp) return 0;
if (fseek(fp, 0, SEEK_END) != 0) { fclose(fp); return 0; }
size = ftell(fp);
if (size < 0) { fclose(fp); return 0; }
if (fseek(fp, 0, SEEK_SET) != 0) { fclose(fp); return 0; }
data = (uint8_t *)TINYGLTF3_MALLOC((size_t)size);
if (!data) { fclose(fp); return 0; }
nread = fread(data, 1, (size_t)size, fp);
fclose(fp);
if (nread != (size_t)size) { TINYGLTF3_FREE(data); return 0; }
*out_data = data;
*out_size = (uint64_t)size;
return 1;
}
static void tg3__fs_free_file(uint8_t *data, uint64_t size, void *ud) {
(void)size; (void)ud; TINYGLTF3_FREE(data);
}
static int32_t tg3__fs_write_file(const char *path, uint32_t path_len,
const uint8_t *data, uint64_t size, void *ud) {
FILE *fp;
size_t nwritten;
(void)path_len; (void)ud;
fp = fopen(path, "wb");
if (!fp) return 0;
nwritten = fwrite(data, 1, (size_t)size, fp);
fclose(fp);
return nwritten == (size_t)size ? 1 : 0;
}
static void tg3__set_default_fs(tg3_fs_callbacks *fs) {
if (!fs->read_file) fs->read_file = tg3__fs_read_file;
if (!fs->free_file) fs->free_file = tg3__fs_free_file;
if (!fs->file_exists) fs->file_exists = tg3__fs_file_exists;
if (!fs->write_file) fs->write_file = tg3__fs_write_file;
}
#endif
static void tg3__model_init(tg3_model *model) {
TINYGLTF3_MEMSET(model, 0, sizeof(*model));
model->default_scene = -1;
}
static int tg3__json_is_number(const tg3json_value *v) {
return v && (v->type == TG3JSON_INT || v->type == TG3JSON_REAL);
}
static int tg3__json_number_to_int32(const tg3json_value *v, int32_t *out) {
double real;
int32_t converted;
if (!tg3__json_is_number(v) || !out) return 0;
if (v->type == TG3JSON_INT) {
if (v->u.integer < INT32_MIN || v->u.integer > INT32_MAX) return 0;
*out = (int32_t)v->u.integer;
return 1;
}
real = v->u.real;
if (!tg3__isfinite(real) || real < (double)INT32_MIN || real > (double)INT32_MAX) {
return 0;
}
converted = (int32_t)real;
if ((double)converted != real) return 0;
*out = converted;
return 1;
}
static int tg3__json_number_to_uint64(const tg3json_value *v, uint64_t *out) {
double real;
uint64_t converted;
/* Doubles have a 53-bit mantissa, so 2^64 itself rounds up and would be UB
on cast to uint64_t. Cap at the largest representable value strictly
below 2^64 (== 2^64 - 2^11). */
const double max_safe_uint64_real = 18446744073709547520.0;
if (!tg3__json_is_number(v) || !out) return 0;
if (v->type == TG3JSON_INT) {
if (v->u.integer < 0) return 0;
*out = (uint64_t)v->u.integer;
return 1;
}
real = v->u.real;
if (!tg3__isfinite(real) || real < 0.0 || real > max_safe_uint64_real) {
return 0;
}
converted = (uint64_t)real;
if ((double)converted != real) return 0;
*out = converted;
return 1;
}
static double tg3__json_number_to_double(const tg3json_value *v) {
if (!v) return 0.0;
if (v->type == TG3JSON_INT) return (double)v->u.integer;
if (v->type == TG3JSON_REAL) return v->u.real;
return 0.0;
}
static int tg3__json_is_object(const tg3json_value *v) { return v && v->type == TG3JSON_OBJECT; }
static int tg3__json_is_array(const tg3json_value *v) { return v && v->type == TG3JSON_ARRAY; }
static int tg3__u64_add_overflow(uint64_t a, uint64_t b, uint64_t *out) {
if (a > UINT64_MAX - b) return 1;
*out = a + b;
return 0;
}
static int tg3__u64_mul_overflow(uint64_t a, uint64_t b, uint64_t *out) {
if (a != 0 && b > UINT64_MAX / a) return 1;
*out = a * b;
return 0;
}
static int tg3__u64_fits_size(uint64_t v) {
#if SIZE_MAX < UINT64_MAX
return v <= (uint64_t)SIZE_MAX;
#else
(void)v;
return 1;
#endif
}
static int tg3__json_has(const tg3json_value *o, const char *key) {
return tg3json_object_get(o, key) ? 1 : 0;
}
static const tg3json_value *tg3__json_get(const tg3json_value *o, const char *key) {
return tg3json_object_get(o, key);
}
static int tg3__parse_string(tg3__parse_ctx *ctx, const tg3json_value *o, const char *key,
tg3_str *out, int required, const char *parent) {
const tg3json_value *it = tg3__json_get(o, key);
if (!it) {
if (required) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_MISSING_FIELD,
parent, "Missing required field '%s'", key);
return 0;
}
out->data = NULL;
out->len = 0;
return 1;
}
if (it->type != TG3JSON_STRING) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_TYPE_MISMATCH,
parent, "Field '%s' must be a string", key);
return 0;
}
*out = tg3__arena_str(ctx->arena, it->u.string.ptr, (uint32_t)it->u.string.len);
return out->data != NULL || it->u.string.len == 0;
}
static int tg3__parse_int(tg3__parse_ctx *ctx, const tg3json_value *o, const char *key,
int32_t *out, int required, const char *parent) {
const tg3json_value *it = tg3__json_get(o, key);
if (!it) {
if (required) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_MISSING_FIELD,
parent, "Missing required field '%s'", key);
return 0;
}
return 1;
}
if (!tg3__json_is_number(it)) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_TYPE_MISMATCH,
parent, "Field '%s' must be a number", key);
return 0;
}
if (!tg3__json_number_to_int32(it, out)) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_TYPE_MISMATCH,
parent, "Field '%s' must be a finite int32-valued number", key);
return 0;
}
return 1;
}
static int tg3__parse_uint64(tg3__parse_ctx *ctx, const tg3json_value *o, const char *key,
uint64_t *out, int required, const char *parent) {
const tg3json_value *it = tg3__json_get(o, key);
if (!it) {
if (required) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_MISSING_FIELD,
parent, "Missing required field '%s'", key);
return 0;
}
return 1;
}
if (!tg3__json_is_number(it)) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_TYPE_MISMATCH,
parent, "Field '%s' must be a number", key);
return 0;
}
if (!tg3__json_number_to_uint64(it, out)) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_TYPE_MISMATCH,
parent, "Field '%s' must be a finite uint64-valued number", key);
return 0;
}
return 1;
}
static int tg3__parse_double(tg3__parse_ctx *ctx, const tg3json_value *o, const char *key,
double *out, int required, const char *parent) {
const tg3json_value *it = tg3__json_get(o, key);
if (!it) {
if (required) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_MISSING_FIELD,
parent, "Missing required field '%s'", key);
return 0;
}
return 1;
}
if (!tg3__json_is_number(it)) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_TYPE_MISMATCH,
parent, "Field '%s' must be a number", key);
return 0;
}
*out = tg3__json_number_to_double(it);
return 1;
}
static int tg3__parse_bool(tg3__parse_ctx *ctx, const tg3json_value *o, const char *key,
int32_t *out, int required, const char *parent) {
const tg3json_value *it = tg3__json_get(o, key);
if (!it) {
if (required) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_MISSING_FIELD,
parent, "Missing required field '%s'", key);
return 0;
}
return 1;
}
if (it->type != TG3JSON_BOOL) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_TYPE_MISMATCH,
parent, "Field '%s' must be a boolean", key);
return 0;
}
*out = it->u.boolean ? 1 : 0;
return 1;
}
static int tg3__parse_number_array(tg3__parse_ctx *ctx, const tg3json_value *o, const char *key,
const double **out, uint32_t *out_count,
int required, const char *parent) {
const tg3json_value *it = tg3__json_get(o, key);
size_t count;
size_t i;
double *arr;
if (!it) {
if (required) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_MISSING_FIELD,
parent, "Missing required field '%s'", key);
return 0;
}
*out = NULL;
*out_count = 0;
return 1;
}
if (!tg3__json_is_array(it)) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_TYPE_MISMATCH,
parent, "Field '%s' must be an array", key);
return 0;
}
count = tg3json_array_size(it);
if (count == 0) {
*out = NULL;
*out_count = 0;
return 1;
}
arr = (double *)tg3__arena_alloc(ctx->arena, count * sizeof(double));
if (!arr) {
tg3__error_push(ctx->errors, TG3_SEVERITY_ERROR, TG3_ERR_OUT_OF_MEMORY,
"OOM allocating number array", parent, -1);
return 0;
}
for (i = 0; i < count; ++i) {
arr[i] = tg3__json_number_to_double(tg3json_array_get(it, i));
}
*out = arr;
*out_count = (uint32_t)count;
return 1;
}
static int tg3__parse_int_array(tg3__parse_ctx *ctx, const tg3json_value *o, const char *key,
const int32_t **out, uint32_t *out_count,
int required, const char *parent) {
const tg3json_value *it = tg3__json_get(o, key);
size_t count;
size_t i;
int32_t *arr;
if (!it) {
if (required) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_MISSING_FIELD,
parent, "Missing required field '%s'", key);
return 0;
}
*out = NULL;
*out_count = 0;
return 1;
}
if (!tg3__json_is_array(it)) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_TYPE_MISMATCH,
parent, "Field '%s' must be an array", key);
return 0;
}
count = tg3json_array_size(it);
if (count == 0) {
*out = NULL;
*out_count = 0;
return 1;
}
arr = (int32_t *)tg3__arena_alloc(ctx->arena, count * sizeof(int32_t));
if (!arr) {
tg3__error_push(ctx->errors, TG3_SEVERITY_ERROR, TG3_ERR_OUT_OF_MEMORY,
"OOM allocating int array", parent, -1);
return 0;
}
for (i = 0; i < count; ++i) {
const tg3json_value *item = tg3json_array_get(it, i);
if (!item || !tg3__json_number_to_int32(item, &arr[i])) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR,
TG3_ERR_JSON_TYPE_MISMATCH, parent,
"Field '%s' element %u must be a finite int32-valued number",
key, (unsigned)i);
return 0;
}
}
*out = arr;
*out_count = (uint32_t)count;
return 1;
}
static void tg3__parse_number_to_fixed(const tg3json_value *o, const char *key,
double *out, uint32_t max_count) {
const tg3json_value *it = tg3__json_get(o, key);
size_t count;
size_t i;
if (!tg3__json_is_array(it)) return;
count = tg3json_array_size(it);
if (count > max_count) count = max_count;
for (i = 0; i < count; ++i) {
const tg3json_value *item = tg3json_array_get(it, i);
if (!item) continue;
out[i] = tg3__json_number_to_double(item);
}
}
static int tg3__parse_string_array(tg3__parse_ctx *ctx, const tg3json_value *o, const char *key,
const tg3_str **out, uint32_t *out_count,
int required, const char *parent) {
const tg3json_value *it = tg3__json_get(o, key);
size_t count;
size_t i;
tg3_str *arr;
if (!it) {
if (required) {
tg3__error_pushf(ctx->errors, ctx->arena, TG3_SEVERITY_ERROR, TG3_ERR_JSON_MISSING_FIELD,
parent, "Missing required field '%s'", key);
return 0;
}
*out = NULL;
*out_count = 0;
return 1;
}
if (!tg3__json_is_array(it)) return 0;
count = tg3json_array_size(it);
if (count == 0) {
*out = NULL;
*out_count = 0;
return 1;
}
arr = (tg3_str *)tg3__arena_alloc(ctx->arena, count * sizeof(tg3_str));
if (!arr) return 0;
for (i = 0; i < count; ++i) {
const tg3json_value *item = tg3json_array_get(it, i);
if (!item || item->type != TG3JSON_STRING) {
arr[i].data = NULL;
arr[i].len = 0;
continue;
}
arr[i] = tg3__arena_str(ctx->arena, item->u.string.ptr, (uint32_t)item->u.string.len);
}
*out = arr;
*out_count = (uint32_t)count;
return 1;
}
static tg3_value tg3__json_to_value(tg3__parse_ctx *ctx, const tg3json_value *j) {
tg3_value v;
size_t i;
TINYGLTF3_MEMSET(&v, 0, sizeof(v));
if (!j) return v;
switch (j->type) {
case TG3JSON_NULL:
v.type = TG3_VALUE_NULL;
break;
case TG3JSON_BOOL:
v.type = TG3_VALUE_BOOL;
v.bool_val = j->u.boolean ? 1 : 0;
break;
case TG3JSON_INT:
v.type = TG3_VALUE_INT;
v.int_val = j->u.integer;
break;
case TG3JSON_REAL:
v.type = TG3_VALUE_REAL;
v.real_val = j->u.real;
break;
case TG3JSON_STRING:
v.type = TG3_VALUE_STRING;
v.string_val = tg3__arena_str(ctx->arena, j->u.string.ptr, (uint32_t)j->u.string.len);
break;
case TG3JSON_ARRAY:
v.type = TG3_VALUE_ARRAY;
if (j->u.array.count > 0) {
tg3_value *arr = (tg3_value *)tg3__arena_alloc(ctx->arena, j->u.array.count * sizeof(tg3_value));
if (arr) {
for (i = 0; i < j->u.array.count; ++i) arr[i] = tg3__json_to_value(ctx, &j->u.array.items[i]);
v.array_data = arr;
v.array_count = (uint32_t)j->u.array.count;
}
}
break;
case TG3JSON_OBJECT:
v.type = TG3_VALUE_OBJECT;
if (j->u.object.count > 0) {
tg3_kv_pair *pairs = (tg3_kv_pair *)tg3__arena_alloc(ctx->arena, j->u.object.count * sizeof(tg3_kv_pair));
if (pairs) {
for (i = 0; i < j->u.object.count; ++i) {
pairs[i].key = tg3__arena_str(ctx->arena, j->u.object.items[i].key,
(uint32_t)j->u.object.items[i].key_len);
pairs[i].value = tg3__json_to_value(ctx, j->u.object.items[i].value);
}
v.object_data = pairs;
v.object_count = (uint32_t)j->u.object.count;
}
}
break;
}
return v;
}
static void tg3__parse_extras_and_extensions(tg3__parse_ctx *ctx, const tg3json_value *o,
tg3_extras_ext *ee) {
const tg3json_value *extras_it;
const tg3json_value *ext_it;
TINYGLTF3_MEMSET(ee, 0, sizeof(*ee));
extras_it = tg3__json_get(o, "extras");
if (extras_it) {
if (!ctx->opts.skip_extras_values) {
tg3_value *ev = (tg3_value *)tg3__arena_alloc(ctx->arena, sizeof(tg3_value));
if (ev) {
*ev = tg3__json_to_value(ctx, extras_it);
ee->extras = ev;
}
}
if (ctx->opts.store_original_json) {
size_t raw_len = 0;
char *raw = tg3json_stringify(extras_it, &raw_len);
if (raw) {
ee->extras_json = tg3__arena_str(ctx->arena, raw, (uint32_t)raw_len);
TINYGLTF3_FREE(raw);
}
}
}
ext_it = tg3__json_get(o, "extensions");
if (tg3__json_is_object(ext_it)) {
size_t count = tg3json_object_size(ext_it);
size_t i;
if (count > 0) {
tg3_extension *exts = (tg3_extension *)tg3__arena_alloc(ctx->arena, count * sizeof(tg3_extension));