/
load.c
1071 lines (994 loc) · 24.5 KB
/
load.c
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
/* load.c
* Copyright (c) 2012, Peter Ohler
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of Peter Ohler nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if SAFE_CACHE
#include <pthread.h>
#endif
#if !IS_WINDOWS
#include <sys/resource.h> // for getrlimit() on linux
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
//Workaround:
#ifndef INFINITY
#define INFINITY (1.0/0.0)
#endif
#include "oj.h"
enum {
TIME_HINT = 0x0100,
};
typedef struct _CircArray {
VALUE obj_array[1024];
VALUE *objs;
unsigned long size; // allocated size or initial array size
unsigned long cnt;
} *CircArray;
typedef struct _ParseInfo {
char *str; /* buffer being read from */
char *s; /* current position in buffer */
CircArray circ_array;
Options options;
char *stack_min;
} *ParseInfo;
static CircArray circ_array_new(void);
static void circ_array_free(CircArray ca);
static void circ_array_set(CircArray ca, VALUE obj, unsigned long id);
static VALUE circ_array_get(CircArray ca, unsigned long id);
static VALUE classname2class(const char *name, ParseInfo pi);
static VALUE read_next(ParseInfo pi, int hint);
static VALUE read_obj(ParseInfo pi);
static VALUE read_array(ParseInfo pi, int hint);
static VALUE read_str(ParseInfo pi, int hint);
static VALUE read_num(ParseInfo pi);
static VALUE read_time(ParseInfo pi);
static VALUE read_true(ParseInfo pi);
static VALUE read_false(ParseInfo pi);
static VALUE read_nil(ParseInfo pi);
static void next_non_white(ParseInfo pi);
static char* read_quoted_value(ParseInfo pi);
static void skip_comment(ParseInfo pi);
/* This XML parser is a single pass, destructive, callback parser. It is a
* single pass parse since it only make one pass over the characters in the
* XML document string. It is destructive because it re-uses the content of
* the string for values in the callback and places \0 characters at various
* places to mark the end of tokens and strings. It is a callback parser like
* a SAX parser because it uses callback when document elements are
* encountered.
*
* Parsing is very tolerant. Lack of headers and even mispelled element
* endings are passed over without raising an error. A best attempt is made in
* all cases to parse the string.
*/
inline static void
next_non_white(ParseInfo pi) {
for (; 1; pi->s++) {
switch(*pi->s) {
case ' ':
case '\t':
case '\f':
case '\n':
case '\r':
break;
case '/':
skip_comment(pi);
break;
default:
return;
}
}
}
inline static void
next_white(ParseInfo pi) {
for (; 1; pi->s++) {
switch(*pi->s) {
case ' ':
case '\t':
case '\f':
case '\n':
case '\r':
case '\0':
return;
default:
break;
}
}
}
inline static VALUE
resolve_classname(VALUE mod, const char *class_name, int auto_define) {
VALUE clas;
ID ci = rb_intern(class_name);
if (rb_const_defined_at(mod, ci)) {
clas = rb_const_get_at(mod, ci);
} else if (auto_define) {
clas = rb_define_class_under(mod, class_name, oj_bag_class);
} else {
clas = Qundef;
}
return clas;
}
inline static VALUE
classname2obj(const char *name, ParseInfo pi) {
VALUE clas = classname2class(name, pi);
if (Qundef == clas) {
return Qnil;
} else {
return rb_obj_alloc(clas);
}
}
static VALUE
classname2class(const char *name, ParseInfo pi) {
VALUE clas;
VALUE *slot;
int auto_define = (Yes == pi->options->auto_define);
#if SAFE_CACHE
pthread_mutex_lock(&oj_cache_mutex);
#endif
if (Qundef == (clas = oj_cache_get(oj_class_cache, name, &slot))) {
char class_name[1024];
char *end = class_name + sizeof(class_name) - 1;
char *s;
const char *n = name;
clas = rb_cObject;
for (s = class_name; '\0' != *n; n++) {
if (':' == *n) {
*s = '\0';
n++;
if (':' != *n) {
raise_error("Invalid classname, expected another ':'", pi->str, pi->s);
}
if (Qundef == (clas = resolve_classname(clas, class_name, auto_define))) {
char buf[1024];
snprintf(buf, sizeof(buf) - 1, "Class %s not defined", class_name);
raise_error(buf, pi->str, pi->s);
}
s = class_name;
} else if (end <= s) {
raise_error("Invalid classname, limit is 1024 characters", pi->str, pi->s);
} else {
*s++ = *n;
}
}
*s = '\0';
if (Qundef != (clas = resolve_classname(clas, class_name, auto_define))) {
*slot = clas;
}
}
#if SAFE_CACHE
pthread_mutex_unlock(&oj_cache_mutex);
#endif
return clas;
}
#if HAS_RSTRUCT
inline static VALUE
structname2obj(const char *name) {
VALUE ost;
ost = rb_const_get(oj_struct_class, rb_intern(name));
// use encoding as the indicator for Ruby 1.8.7 or 1.9.x
#if HAS_ENCODING_SUPPORT
return rb_struct_alloc_noinit(ost);
#else
return rb_struct_new(ost);
#endif
}
#endif
inline static unsigned long
read_ulong(const char *s, ParseInfo pi) {
unsigned long n = 0;
for (; '\0' != *s; s++) {
if ('0' <= *s && *s <= '9') {
n = n * 10 + (*s - '0');
} else {
raise_error("Not a valid ID number", pi->str, pi->s);
}
}
return n;
}
static CircArray
circ_array_new() {
CircArray ca;
if (0 == (ca = ALLOC(struct _CircArray))) {
rb_raise(rb_eNoMemError, "not enough memory\n");
}
ca->objs = ca->obj_array;
ca->size = sizeof(ca->obj_array) / sizeof(VALUE);
ca->cnt = 0;
return ca;
}
static void
circ_array_free(CircArray ca) {
if (ca->objs != ca->obj_array) {
xfree(ca->objs);
}
xfree(ca);
}
static void
circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
if (0 < id && 0 != ca) {
unsigned long i;
if (ca->size < id) {
unsigned long cnt = id + 512;
if (ca->objs == ca->obj_array) {
if (0 == (ca->objs = ALLOC_N(VALUE, cnt))) {
rb_raise(rb_eNoMemError, "not enough memory\n");
}
memcpy(ca->objs, ca->obj_array, sizeof(VALUE) * ca->cnt);
} else {
REALLOC_N(ca->objs, VALUE, cnt);
}
ca->size = cnt;
}
id--;
for (i = ca->cnt; i < id; i++) {
ca->objs[i] = Qnil;
}
ca->objs[id] = obj;
if (ca->cnt <= id) {
ca->cnt = id + 1;
}
}
}
static VALUE
circ_array_get(CircArray ca, unsigned long id) {
VALUE obj = Qnil;
if (id <= ca->cnt && 0 != ca) {
obj = ca->objs[id - 1];
}
return obj;
}
static void
skip_comment(ParseInfo pi) {
pi->s++; // skip first /
if ('*' == *pi->s) {
pi->s++;
for (; '\0' != *pi->s; pi->s++) {
if ('*' == *pi->s && '/' == *(pi->s + 1)) {
pi->s++;
return;
} else if ('\0' == *pi->s) {
raise_error("comment not terminated", pi->str, pi->s);
}
}
} else if ('/' == *pi->s) {
for (; 1; pi->s++) {
switch (*pi->s) {
case '\n':
case '\r':
case '\f':
case '\0':
return;
default:
break;
}
}
} else {
raise_error("invalid comment", pi->str, pi->s);
}
}
static VALUE
read_next(ParseInfo pi, int hint) {
VALUE obj;
if ((char*)&obj < pi->stack_min) {
rb_raise(rb_eSysStackError, "JSON is too deeply nested");
}
next_non_white(pi); // skip white space
switch (*pi->s) {
case '{':
obj = read_obj(pi);
break;
case '[':
obj = read_array(pi, hint);
break;
case '"':
obj = read_str(pi, hint);
break;
case '+':
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (TIME_HINT == hint) {
obj = read_time(pi);
} else {
obj = read_num(pi);
}
break;
case 'I':
obj = read_num(pi);
break;
case 't':
obj = read_true(pi);
break;
case 'f':
obj = read_false(pi);
break;
case 'n':
obj = read_nil(pi);
break;
case '\0':
obj = Qundef;
break;
default:
obj = Qundef;
break;
}
return obj;
}
static VALUE
read_obj(ParseInfo pi) {
VALUE obj = Qundef;
VALUE key = Qundef;
VALUE val = Qundef;
const char *ks;
int obj_type = T_NONE;
const char *json_class_name = 0;
Mode mode = pi->options->mode;
Odd odd = 0;
VALUE odd_args[MAX_ODD_ARGS];
VALUE *vp;
pi->s++;
next_non_white(pi);
if ('}' == *pi->s) {
pi->s++;
return rb_hash_new();
}
while (1) {
next_non_white(pi);
ks = 0;
key = Qundef;
val = Qundef;
if ('"' != *pi->s || Qundef == (key = read_str(pi, 0))) {
raise_error("unexpected character", pi->str, pi->s);
}
next_non_white(pi);
if (':' == *pi->s) {
pi->s++;
} else {
raise_error("invalid format, expected :", pi->str, pi->s);
}
if (T_STRING == rb_type(key)) {
ks = StringValuePtr(key);
} else {
ks = 0;
}
if (0 != ks && Qundef == obj && ObjectMode == mode) {
if ('^' == *ks && '\0' == ks[2]) { // special directions
switch (ks[1]) {
case 't': // Time
obj = read_next(pi, TIME_HINT); // raises if can not convert to Time
key = Qundef;
break;
case 'c': // Class
obj = read_next(pi, T_CLASS);
key = Qundef;
break;
case 's': // String
obj = read_next(pi, T_STRING);
key = Qundef;
break;
case 'm': // Symbol
obj = read_next(pi, T_SYMBOL);
key = Qundef;
break;
case 'o': // Object
obj = read_next(pi, T_OBJECT);
obj_type = T_OBJECT;
key = Qundef;
break;
case 'O': // Odd class
if (0 == (odd = oj_get_odd(read_next(pi, T_CLASS)))) {
raise_error("Not a valid build in class.", pi->str, pi->s);
}
obj = Qundef;
key = Qundef;
for (vp = odd_args + MAX_ODD_ARGS - 1; odd_args <=vp; vp--) {
*vp = Qnil;
}
break;
case 'u': // Struct
obj = read_next(pi, T_STRUCT);
obj_type = T_STRUCT;
key = Qundef;
break;
default:
// handle later
break;
}
}
}
if (Qundef != key) {
if (Qundef == val && Qundef == (val = read_next(pi, 0))) {
raise_error("unexpected character", pi->str, pi->s);
}
if (Qundef == obj && 0 == odd) {
obj = rb_hash_new();
obj_type = T_HASH;
}
if (ObjectMode == mode && 0 != ks && '^' == *ks) {
int val_type = rb_type(val);
if ('i' == ks[1] && '\0' == ks[2] && T_FIXNUM == val_type) {
circ_array_set(pi->circ_array, obj, NUM2ULONG(val));
key = Qundef;
} else if ('#' == ks[1] &&
(T_NONE == obj_type || T_HASH == obj_type) &&
T_ARRAY == val_type && 2 == RARRAY_LEN(val)) { // Hash entry
VALUE *np = RARRAY_PTR(val);
key = *np;
val = *(np + 1);
}
}
if (Qundef != key) {
if (0 != odd) {
ID *idp;
for (idp = odd->attrs, vp = odd_args; 0 != *idp; idp++, vp++) {
if (0 == strcmp(rb_id2name(*idp), ks)) {
*vp = val;
break;
}
}
if (odd_args + MAX_ODD_ARGS <= vp) {
raise_error("invalid attribute", pi->str, pi->s);
}
} else if (T_OBJECT == obj_type) {
VALUE *slot;
ID var_id;
#if SAFE_CACHE
pthread_mutex_lock(&oj_cache_mutex);
#endif
if (Qundef == (var_id = oj_cache_get(oj_attr_cache, ks, &slot))) {
char attr[1024];
if ('~' == *ks) {
strncpy(attr, ks + 1, sizeof(attr) - 1);
} else {
*attr = '@';
strncpy(attr + 1, ks, sizeof(attr) - 2);
}
attr[sizeof(attr) - 1] = '\0';
var_id = rb_intern(attr);
*slot = var_id;
}
#if SAFE_CACHE
pthread_mutex_unlock(&oj_cache_mutex);
#endif
#if HAS_EXCEPTION_MAGIC
if ('~' == *ks && Qtrue == rb_obj_is_kind_of(obj, rb_eException)) {
if (0 == strcmp("~mesg", ks)) {
VALUE args[1];
args[0] = val;
obj = rb_class_new_instance(1, args, rb_class_of(obj));
} else if (0 == strcmp("~bt", ks)) {
rb_funcall(obj, rb_intern("set_backtrace"), 1, val);
}
} else {
rb_ivar_set(obj, var_id, val);
}
#else
rb_ivar_set(obj, var_id, val);
#endif
} else if (T_HASH == obj_type) {
if (Yes == pi->options->sym_key && T_STRING == rb_type(key)) {
rb_hash_aset(obj, rb_str_intern(key), val);
} else {
rb_hash_aset(obj, key, val);
}
if ((CompatMode == mode || ObjectMode == mode) &&
0 == json_class_name && 0 != ks &&
0 != pi->options->create_id && *pi->options->create_id == *ks && 0 == strcmp(pi->options->create_id, ks) &&
T_STRING == rb_type(val)) {
json_class_name = StringValuePtr(val);
}
} else {
raise_error("invalid Object format, too many Hash entries.", pi->str, pi->s);
}
}
}
next_non_white(pi);
if ('}' == *pi->s) {
pi->s++;
break;
} else if (',' == *pi->s) {
pi->s++;
} else {
//printf("*** '%s'\n", pi->s);
raise_error("invalid format, expected , or } while in an object", pi->str, pi->s);
}
}
if (0 != odd) {
obj = rb_funcall2(odd->create_obj, odd->create_op, odd->attr_cnt, odd_args);
} else if (0 != json_class_name) {
VALUE clas = classname2class(json_class_name, pi);
VALUE args[1];
*args = obj;
obj = rb_funcall2(clas, oj_json_create_id, 1, args);
}
return obj;
}
static VALUE
read_array(ParseInfo pi, int hint) {
VALUE a = Qundef;
VALUE e;
int type = T_NONE;
int cnt = 0;
int a_str;
#if HAS_RSTRUCT
long slen = 0;
#endif
pi->s++;
next_non_white(pi);
if (']' == *pi->s) {
pi->s++;
return rb_ary_new();
}
while (1) {
next_non_white(pi);
a_str = ('"' == *pi->s);
if (Qundef == (e = read_next(pi, 0))) {
raise_error("unexpected character", pi->str, pi->s);
}
#if HAS_RSTRUCT
if (Qundef == a && T_STRUCT == hint && T_STRING == rb_type(e)) {
a = structname2obj(StringValuePtr(e));
type = T_STRUCT;
slen = RSTRUCT_LEN(a);
e = Qundef;
}
#endif
if (Qundef == a) {
a = rb_ary_new();
type = T_ARRAY;
}
if (a_str && T_FIXNUM == rb_type(e)) {
circ_array_set(pi->circ_array, a, NUM2ULONG(e));
e = Qundef;
}
if (Qundef != e) {
if (T_STRUCT == type) {
#if HAS_RSTRUCT
if (slen <= cnt) {
raise_error("Too many elements for Struct", pi->str, pi->s);
}
RSTRUCT_PTR(a)[cnt] = e;
#else
raise_error("Ruby structs not supported with this version of Ruby", pi->str, pi->s);
#endif
} else {
rb_ary_push(a, e);
}
cnt++;
}
next_non_white(pi);
if (',' == *pi->s) {
pi->s++;
} else if (']' == *pi->s) {
pi->s++;
break;
} else {
raise_error("invalid format, expected , or ] while in an array", pi->str, pi->s);
}
}
return a;
}
static VALUE
read_str(ParseInfo pi, int hint) {
char *text;
VALUE obj;
int escaped;
escaped = ('\\' == pi->s[1]);
text = read_quoted_value(pi);
if (ObjectMode != pi->options->mode) {
hint = T_STRING;
}
switch (hint) {
case T_CLASS:
obj = classname2class(text, pi);
break;
case T_OBJECT:
obj = classname2obj(text, pi);
break;
case T_STRING:
obj = rb_str_new2(text);
#if HAS_ENCODING_SUPPORT
rb_enc_associate(obj, oj_utf8_encoding);
#endif
break;
case T_SYMBOL:
#if HAS_ENCODING_SUPPORT
obj = rb_str_new2(text);
rb_enc_associate(obj, oj_utf8_encoding);
obj = rb_funcall(obj, oj_to_sym_id, 0);
#else
obj = ID2SYM(rb_intern(text));
#endif
break;
case 0:
default:
obj = Qundef;
if (':' == *text && !escaped) { // Symbol
#if HAS_ENCODING_SUPPORT
obj = rb_str_new2(text + 1);
rb_enc_associate(obj, oj_utf8_encoding);
obj = rb_funcall(obj, oj_to_sym_id, 0);
#else
obj = ID2SYM(rb_intern(text + 1));
#endif
} else if (ObjectMode == pi->options->mode && '^' == *text && '\0' != text[2]) {
char c1 = text[1];
if ('r' == c1 && 0 != pi->circ_array) {
obj = circ_array_get(pi->circ_array, read_ulong(text + 2, pi));
} else if ('i' == c1) {
obj = ULONG2NUM(read_ulong(text + 2, pi));
}
}
if (Qundef == obj) {
obj = rb_str_new2(text);
#if HAS_ENCODING_SUPPORT
rb_enc_associate(obj, oj_utf8_encoding);
#endif
}
break;
}
return obj;
}
#ifdef RUBINIUS_RUBY
#define NUM_MAX 0x07FFFFFF
#else
#define NUM_MAX (FIXNUM_MAX >> 8)
#endif
static VALUE
read_num(ParseInfo pi) {
char *start = pi->s;
int64_t n = 0;
long a = 0;
long div = 1;
long e = 0;
int neg = 0;
int eneg = 0;
int big = 0;
if ('-' == *pi->s) {
pi->s++;
neg = 1;
} else if ('+' == *pi->s) {
pi->s++;
}
if ('I' == *pi->s) {
if (0 != strncmp("Infinity", pi->s, 8)) {
raise_error("number or other value", pi->str, pi->s);
}
pi->s += 8;
if (neg) {
return rb_float_new(-INFINITY);
} else {
return rb_float_new(INFINITY);
}
}
for (; '0' <= *pi->s && *pi->s <= '9'; pi->s++) {
if (big) {
big++;
} else {
n = n * 10 + (*pi->s - '0');
if (NUM_MAX <= n) {
big = 1;
}
}
}
if ('.' == *pi->s) {
pi->s++;
for (; '0' <= *pi->s && *pi->s <= '9'; pi->s++) {
a = a * 10 + (*pi->s - '0');
div *= 10;
if (NUM_MAX <= div) {
big = 1;
}
}
}
if ('e' == *pi->s || 'E' == *pi->s) {
pi->s++;
if ('-' == *pi->s) {
pi->s++;
eneg = 1;
} else if ('+' == *pi->s) {
pi->s++;
}
for (; '0' <= *pi->s && *pi->s <= '9'; pi->s++) {
e = e * 10 + (*pi->s - '0');
if (NUM_MAX <= e) {
big = 1;
}
}
}
if (0 == e && 0 == a && 1 == div) {
if (big) {
char c = *pi->s;
VALUE num;
*pi->s = '\0';
num = rb_cstr_to_inum(start, 10, 0);
*pi->s = c;
return num;
} else {
if (neg) {
n = -n;
}
return LONG2NUM(n);
}
} else { // decimal
if (big) {
char c = *pi->s;
VALUE num;
*pi->s = '\0';
num = rb_funcall(oj_bigdecimal_class, oj_new_id, 1, rb_str_new2(start));
*pi->s = c;
return num;
} else {
double d = (double)n + (double)a / (double)div;
if (neg) {
d = -d;
}
if (1 < big) {
e += big - 1;
}
if (0 != e) {
if (eneg) {
e = -e;
}
d *= pow(10.0, e);
}
return rb_float_new(d);
}
}
}
static VALUE
read_time(ParseInfo pi) {
time_t v = 0;
long v2 = 0;
int neg = 0;
if ('-' == *pi->s) {
pi->s++;
neg = 1;
}
for (; '0' <= *pi->s && *pi->s <= '9'; pi->s++) {
v = v * 10 + (*pi->s - '0');
}
if ('.' == *pi->s) {
int cnt;
pi->s++;
for (cnt = 9; 0 < cnt && '0' <= *pi->s && *pi->s <= '9'; pi->s++, cnt--) {
v2 = v2 * 10 + (*pi->s - '0');
}
for (; 0 < cnt; cnt--) {
v2 *= 10;
}
}
if (neg) {
v = -v;
if (0 < v2) {
v--;
v2 = 1000000000 - v2;
}
}
#if HAS_NANO_TIME
return rb_time_nano_new(v, v2);
#else
return rb_time_new(v, v2 / 1000);
#endif
}
static VALUE
read_true(ParseInfo pi) {
pi->s++;
if ('r' != *pi->s || 'u' != *(pi->s + 1) || 'e' != *(pi->s + 2)) {
raise_error("invalid format, expected 'true'", pi->str, pi->s);
}
pi->s += 3;
return Qtrue;
}
static VALUE
read_false(ParseInfo pi) {
pi->s++;
if ('a' != *pi->s || 'l' != *(pi->s + 1) || 's' != *(pi->s + 2) || 'e' != *(pi->s + 3)) {
raise_error("invalid format, expected 'false'", pi->str, pi->s);
}
pi->s += 4;
return Qfalse;
}
static VALUE
read_nil(ParseInfo pi) {
pi->s++;
if ('u' != *pi->s || 'l' != *(pi->s + 1) || 'l' != *(pi->s + 2)) {
raise_error("invalid format, expected 'nil'", pi->str, pi->s);
}
pi->s += 3;
return Qnil;
}
static uint32_t
read_hex(ParseInfo pi, char *h) {
uint32_t b = 0;
int i;
// TBD this can be made faster with a table
for (i = 0; i < 4; i++, h++) {
b = b << 4;
if ('0' <= *h && *h <= '9') {
b += *h - '0';
} else if ('A' <= *h && *h <= 'F') {
b += *h - 'A' + 10;
} else if ('a' <= *h && *h <= 'f') {
b += *h - 'a' + 10;
} else {
pi->s = h;
raise_error("invalid hex character", pi->str, pi->s);
}
}
return b;
}
static char*
unicode_to_chars(ParseInfo pi, char *t, uint32_t code) {
if (0x0000007F >= code) {
*t = (char)code;
} else if (0x000007FF >= code) {
*t++ = 0xC0 | (code >> 6);
*t = 0x80 | (0x3F & code);
} else if (0x0000FFFF >= code) {
*t++ = 0xE0 | (code >> 12);
*t++ = 0x80 | ((code >> 6) & 0x3F);
*t = 0x80 | (0x3F & code);
} else if (0x001FFFFF >= code) {
*t++ = 0xF0 | (code >> 18);
*t++ = 0x80 | ((code >> 12) & 0x3F);
*t++ = 0x80 | ((code >> 6) & 0x3F);
*t = 0x80 | (0x3F & code);
} else if (0x03FFFFFF >= code) {
*t++ = 0xF8 | (code >> 24);
*t++ = 0x80 | ((code >> 18) & 0x3F);
*t++ = 0x80 | ((code >> 12) & 0x3F);
*t++ = 0x80 | ((code >> 6) & 0x3F);
*t = 0x80 | (0x3F & code);
} else if (0x7FFFFFFF >= code) {
*t++ = 0xFC | (code >> 30);
*t++ = 0x80 | ((code >> 24) & 0x3F);
*t++ = 0x80 | ((code >> 18) & 0x3F);
*t++ = 0x80 | ((code >> 12) & 0x3F);
*t++ = 0x80 | ((code >> 6) & 0x3F);
*t = 0x80 | (0x3F & code);
} else {
raise_error("invalid Unicode", pi->str, pi->s);
}
return t;
}
/* Assume the value starts immediately and goes until the quote character is
* reached again. Do not read the character after the terminating quote.
*/
static char*
read_quoted_value(ParseInfo pi) {
char *value = 0;
char *h = pi->s; // head
char *t = h; // tail
uint32_t code;
h++; // skip quote character
t++;
value = h;
for (; '"' != *h; h++, t++) {
if ('\0' == *h) {
pi->s = h;
raise_error("quoted string not terminated", pi->str, pi->s);
} else if ('\\' == *h) {
h++;
switch (*h) {
case 'n': *t = '\n'; break;
case 'r': *t = '\r'; break;
case 't': *t = '\t'; break;
case 'f': *t = '\f'; break;
case 'b': *t = '\b'; break;
case '"': *t = '"'; break;
case '/': *t = '/'; break;
case '\\': *t = '\\'; break;
case 'u':
h++;
code = read_hex(pi, h);
h += 3;
if (0x0000D800 <= code && code <= 0x0000DFFF) {
uint32_t c1 = (code - 0x0000D800) & 0x000003FF;
uint32_t c2;
h++;
if ('\\' != *h || 'u' != *(h + 1)) {
pi->s = h;
raise_error("invalid escaped character", pi->str, pi->s);
}