-
Notifications
You must be signed in to change notification settings - Fork 32
/
detect.c
811 lines (650 loc) · 15.6 KB
/
detect.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
/**
* YAML parser and emitter PHP extension
*
* Copyright (c) 2007 Ryusuke SEKIYAMA. All rights reserved.
* Copyright (c) 2009 Keynetics Inc. All rights reserved.
* Copyright (c) 2015 Bryan Davis and contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* @package php_yaml
* @author Ryusuke SEKIYAMA <[email protected]>
* @author Bryan Davis <[email protected]>
* @copyright 2007 Ryusuke SEKIYAMA
* @copyright 2009 Keynetics Inc
* @copyright 2015 Bryan Davis and contributors
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
#include "php_yaml.h"
#include "php_yaml_int.h"
/* {{{ local macros
*/
#define ts_skip_space() \
while (ptr < end && (*ptr == ' ' || *ptr == '\t')) { \
ptr++; \
}
#define ts_skip_number() \
while (ptr < end && *ptr >= '0' && *ptr <= '9') { \
ptr++; \
}
/* }}} */
/* {{{ local prototypes
*/
static zend_long eval_sexagesimal_l(zend_long lval, const char *sg, const char *eos);
static double eval_sexagesimal_d(double dval, const char *sg, const char *eos);
/* }}} */
/* {{{ detect_scalar_type(const char *, size_t, yaml_event_t)
* Guess what datatype the scalar encodes
*/
const char *detect_scalar_type(const char *value, size_t length,
const yaml_event_t *event)
{
int flags = 0;
zend_long lval = 0;
double dval = 0.0;
/* is value a null? */
if (0 == length || scalar_is_null(value, length, event)) {
return YAML_NULL_TAG;
}
/* is value numeric? */
flags = scalar_is_numeric(value, length, &lval, &dval, NULL);
if (flags != Y_SCALAR_IS_NOT_NUMERIC) {
return (flags & Y_SCALAR_IS_FLOAT) ? YAML_FLOAT_TAG : YAML_INT_TAG;
}
/* is value boolean? */
flags = scalar_is_bool(value, length, event);
if (-1 != flags) {
return YAML_BOOL_TAG;
}
/* is value a timestamp? */
if (scalar_is_timestamp(value, length)) {
return YAML_TIMESTAMP_TAG;
}
/* no guess */
return NULL;
}
/* }}} */
/* {{{ scalar_is_null(const char *,size_t,yaml_event_t)
* Does this scalar encode a NULL value?
*
* specification is found at http://yaml.org/type/null.html.
*/
int
scalar_is_null(const char *value, size_t length, const yaml_event_t *event)
{
if (NULL != event && event->data.scalar.quoted_implicit) {
return 0;
}
if (NULL == event || event->data.scalar.plain_implicit) {
if (length == 0 ||
(length == 1 && *value == '~') ||
STR_EQ("NULL", value) ||
STR_EQ("Null", value) ||
STR_EQ("null", value)) {
return 1;
}
} else if (NULL != event && SCALAR_TAG_IS((*event), YAML_NULL_TAG)) {
return 1;
}
return 0;
}
/* }}} */
/* {{{ scalar_is_bool(const char *,size_t,yaml_event_t)
* Does this scalar encode a BOOL value?
*
* specification is found at http://yaml.org/type/bool.html.
*/
int
scalar_is_bool(const char *value, size_t length, const yaml_event_t *event)
{
/* TODO: add ini setting to turn 'y'/'n' checks on/off */
if (NULL == event || IS_NOT_QUOTED_OR_TAG_IS((*event), YAML_BOOL_TAG)) {
if ((length == 1 && (*value == 'Y' || *value == 'y')) ||
STR_EQ("YES", value) ||
STR_EQ("Yes", value) ||
STR_EQ("yes", value) ||
STR_EQ("TRUE", value) ||
STR_EQ("True", value) ||
STR_EQ("true", value) ||
STR_EQ("ON", value) ||
STR_EQ("On", value) ||
STR_EQ("on", value)) {
return 1;
}
if ((length == 1 && (*value == 'N' || *value == 'n')) ||
STR_EQ("NO", value) ||
STR_EQ("No", value) ||
STR_EQ("no", value) ||
STR_EQ("FALSE", value) ||
STR_EQ("False", value) ||
STR_EQ("false", value) ||
STR_EQ("OFF", value) ||
STR_EQ("Off", value) ||
STR_EQ("off", value)) {
return 0;
}
} else if (NULL != event &&
IS_NOT_IMPLICIT_AND_TAG_IS((*event), YAML_BOOL_TAG)) {
if (0 == length || (1 == length && '0' == *value)) {
return 0;
} else {
return 1;
}
}
return -1;
}
/* }}} */
/* {{{ scalar_is_numeric()
* Does this scalar encode a NUMERIC value?
*
* specification is found at http://yaml.org/type/float.html.
* specification is found at http://yaml.org/type/int.html.
*/
int
scalar_is_numeric(const char *value, size_t length, zend_long *lval,
double *dval, char **str)
{
const char *end = value + length;
char *buf = { 0 }, *ptr = { 0 };
int negative = 0;
int type = 0;
if (0 == length) {
goto not_numeric;
}
/* trim */
while (value < end && (*(end - 1) == ' ' || *(end - 1) == '\t')) {
end--;
}
while (value < end && (*value == ' ' || *value == '\t')) {
value++;
}
if (value == end) {
goto not_numeric;
}
/* not a number */
if (STR_EQ(".NAN", value) ||
STR_EQ(".NaN", value) ||
STR_EQ(".nan", value)) {
type = Y_SCALAR_IS_FLOAT | Y_SCALAR_IS_NAN;
goto finish;
}
/* catch the degenerate case of `.` as input */
if (STR_EQ(".", value)) {
goto not_numeric;
}
/* sign */
if (*value == '+') {
value++;
} else if (*value == '-') {
negative = 1;
value++;
}
if (value == end) {
goto not_numeric;
}
/* infinity */
if (STR_EQ(".INF", value) ||
STR_EQ(".Inf", value) ||
STR_EQ(".inf", value)) {
type = Y_SCALAR_IS_FLOAT;
type |= (negative ? Y_SCALAR_IS_INFINITY_N : Y_SCALAR_IS_INFINITY_P);
goto finish;
}
/* alloc */
buf = (char *) emalloc(length + 3);
ptr = buf;
if (negative) {
*ptr++ = '-';
}
/* parse */
if (*value == '0') {
*ptr++ = *value++;
if (value == end) {
goto return_zero;
}
if (*value == 'b') {
/* binary integer */
*ptr++ = *value++;
if (value == end) {
goto not_numeric;
}
while (value < end && (*value == '_' || *value == '0')) {
value++;
}
if (value == end) {
goto return_zero;
}
/* check the sequence */
while (value < end) {
if (*value == '_') {
value++;
} else if (*value == '0' || *value == '1') {
*ptr++ = *value++;
} else {
goto not_numeric;
}
}
type = Y_SCALAR_IS_INT | Y_SCALAR_IS_BINARY;
} else if (*value == 'x') {
/* hexadecimal integer */
*ptr++ = *value++;
if (value == end) {
goto not_numeric;
}
while (value < end && (*value == '_' || *value == '0')) {
value++;
}
if (value == end) {
goto return_zero;
}
/* check the sequence */
while (value < end) {
if (*value == '_') {
value++;
} else if ((*value >= '0' && *value <= '9') ||
(*value >= 'A' && *value <= 'F') ||
(*value >= 'a' && *value <= 'f')) {
*ptr++ = *value++;
} else {
goto not_numeric;
}
}
type = Y_SCALAR_IS_INT | Y_SCALAR_IS_HEXADECIMAL;
} else if (*value == '_' || (*value >= '0' && *value <= '7')) {
/* octal integer */
while (value < end) {
if (*value == '_') {
value++;
} else if (*value >= '0' && *value <= '7') {
*ptr++ = *value++;
} else {
goto not_numeric;
}
}
type = Y_SCALAR_IS_INT | Y_SCALAR_IS_OCTAL;
} else if (*value == '.') {
goto check_float;
} else if (*value == ':') {
goto check_sexa;
} else {
goto not_numeric;
}
} else if (*value >= '1' && *value <= '9') {
/* integer */
*ptr++ = *value++;
while (value < end) {
if (*value == '_' || *value == ',') {
value++;
} else if (*value >= '0' && *value <= '9') {
*ptr++ = *value++;
} else if (*value == ':') {
goto check_sexa;
} else if (*value == '.') {
goto check_float;
} else {
goto not_numeric;
}
}
type = Y_SCALAR_IS_INT | Y_SCALAR_IS_DECIMAL;
} else if (*value == ':') {
/* sexagecimal */
check_sexa:
while (value < end) {
if (*value == '.') {
type = Y_SCALAR_IS_FLOAT | Y_SCALAR_IS_SEXAGECIMAL;
goto check_float;
}
if (*value != ':') {
goto not_numeric;
}
*ptr++ = *value++;
if (*(value + 1) == ':' || *(value + 1) == '.' ||
(value + 1) == end) {
if (*value >= '0' && *value <= '9') {
*ptr++ = *value++;
} else {
goto not_numeric;
}
} else {
if ((*value >= '0' && *value <= '5') &&
(*(value + 1) >= '0' && *(value + 1) <= '9')) {
*ptr++ = *value++;
*ptr++ = *value++;
} else {
goto not_numeric;
}
}
}
if (*value == '.') {
type = Y_SCALAR_IS_FLOAT | Y_SCALAR_IS_SEXAGECIMAL;
goto check_float;
} else if (value == end) {
type = Y_SCALAR_IS_INT | Y_SCALAR_IS_SEXAGECIMAL;
} else {
goto not_numeric;
}
} else if (*value == '.') {
/* float */
*ptr++ = '0';
check_float:
*ptr++ = *value++;
if (type == (Y_SCALAR_IS_FLOAT | Y_SCALAR_IS_SEXAGECIMAL)) {
/* sexagecimal float */
while (value < end && (*(end - 1) == '_' || *(end - 1) == '0')) {
end--;
}
if (value == end) {
*ptr++ = '0';
}
while (value < end) {
if (*value == '_') {
value++;
} else if (*value >= '0' && *value <= '9') {
*ptr++ = *value++;
} else {
goto not_numeric;
}
}
} else {
/* decimal float */
int is_exp = 0;
while (value < end) {
if (*value == '_') {
value++;
} else if (*value >= '0' && *value <= '9') {
*ptr++ = *value++;
} else if (*value == 'E' || *value == 'e') {
/* exponential */
is_exp = 1;
*ptr++ = *value++;
if (value == end || (*value != '+' && *value != '-')) {
goto not_numeric;
}
*ptr++ = *value++;
if (value == end || *value < '0' || *value > '9' ||
(*value == '0' && value + 1 == end)) {
goto not_numeric;
}
*ptr++ = *value++;
while (value < end) {
if (*value >= '0' && *value <= '9') {
*ptr++ = *value++;
} else {
goto not_numeric;
}
}
} else {
goto not_numeric;
}
}
/* trim */
if (!is_exp) {
while (*(ptr - 1) == '0') {
ptr--;
}
if (*(ptr - 1) == '.') {
*ptr++ = '0';
}
}
type = Y_SCALAR_IS_FLOAT | Y_SCALAR_IS_DECIMAL;
}
} else {
goto not_numeric;
}
/* terminate */
*ptr = '\0';
finish:
/* convert & assign */
if ((type & Y_SCALAR_IS_INT) && lval != NULL) {
switch (type & Y_SCALAR_FORMAT_MASK) {
case Y_SCALAR_IS_BINARY:
ptr = buf + 2;
if (*ptr == 'b') {
ptr++;
}
*lval = ZEND_STRTOL(ptr, (char **) NULL, 2);
if (*buf == '-') {
*lval *= -1L;
}
break;
case Y_SCALAR_IS_OCTAL:
*lval = ZEND_STRTOL(buf, (char **) NULL, 8);
break;
case Y_SCALAR_IS_HEXADECIMAL:
*lval = ZEND_STRTOL(buf, (char **) NULL, 16);
break;
case Y_SCALAR_IS_SEXAGECIMAL:
*lval = eval_sexagesimal_l(0, buf, ptr);
if (*buf == '-') {
*lval *= -1L;
}
break;
default:
#if PHP_VERSION_ID < 80100
ZEND_ATOL(*lval, buf);
#else
*lval = ZEND_ATOL(buf);
#endif
break;
}
} else if ((type & Y_SCALAR_IS_FLOAT) && dval != NULL) {
switch (type & Y_SCALAR_FORMAT_MASK) {
case Y_SCALAR_IS_SEXAGECIMAL:
*dval = eval_sexagesimal_d(0.0, buf, ptr);
if (*buf == '-') {
*dval *= -1.0;
}
break;
case Y_SCALAR_IS_INFINITY_P:
*dval = php_get_inf();
break;
case Y_SCALAR_IS_INFINITY_N:
*dval = -php_get_inf();
break;
case Y_SCALAR_IS_NAN:
*dval = php_get_nan();
break;
default:
*dval = zend_strtod(buf, (const char **) NULL);
break;
}
}
if (buf != NULL) {
if (str != NULL) {
*str = buf;
} else {
efree(buf);
}
}
/* return */
return type;
return_zero:
if (lval != NULL) {
*lval = 0;
}
if (dval != NULL) {
*dval = 0.0;
}
if (buf != NULL) {
efree(buf);
}
return (Y_SCALAR_IS_INT | Y_SCALAR_IS_ZERO);
not_numeric:
if (buf != NULL) {
efree(buf);
}
return Y_SCALAR_IS_NOT_NUMERIC;
}
/* }}} */
/* {{{ scalar_is_timestamp(const char *,size_t)
* Does this scalar encode a TIMESTAMP value?
*
* specification is found at http://yaml.org/type/timestamp.html.
*/
int scalar_is_timestamp(const char *value, size_t length)
{
const char *ptr = value;
const char *end = value + length;
const char *pos1, *pos2;
if (ptr == NULL || ptr == end) {
return 0;
}
/* skip leading space */
ts_skip_space();
/* check 4 digit year and separator */
pos1 = pos2 = ptr;
ts_skip_number();
if (ptr == pos1 || ptr == end || ptr - pos1 != 4 || *ptr != '-') {
return 0;
}
/* check 1 or 2 month and separator */
pos2 = ++ptr;
ts_skip_number();
if (ptr == pos2 || ptr == end || ptr - pos2 > 2 || *ptr != '-') {
return 0;
}
/* check 1 or 2 digit day */
pos2 = ++ptr;
ts_skip_number();
if (ptr == pos2 || ptr - pos2 > 2) {
return 0;
}
/* check separator */
pos2 = ptr;
if (ptr == end) {
/* date only format requires YYYY-MM-DD */
return (pos2 - pos1 == 10) ? 1 : 0;
}
/* time separator is T or whitespace */
if (*ptr == 'T' || *ptr == 't') {
ptr++;
} else if (*ptr == ' ' || *ptr == '\t') {
ts_skip_space();
} else {
return 0;
}
/* check 1 or 2 digit hour and separator */
pos1 = ptr;
ts_skip_number();
if (ptr == pos1 || ptr == end || ptr - pos1 > 2 || *ptr != ':') {
return 0;
}
/* check 2 digit minute and separator */
pos1 = ++ptr;
ts_skip_number();
if (ptr == end || ptr - pos1 != 2 || *ptr != ':') {
return 0;
}
/* check 2 digit second */
pos1 = ++ptr;
ts_skip_number();
if (ptr == end) {
return (ptr - pos1 == 2) ? 1 : 0;
}
/* check optional fraction */
if (*ptr == '.') {
ptr++;
ts_skip_number();
}
/* skip optional separator space */
ts_skip_space();
if (ptr == end) {
return 1;
}
/* check time zone */
if (*ptr == 'Z') {
ptr++;
ts_skip_space();
return (ptr == end) ? 1 : 0;
}
/* check time zone offset sign */
if (*ptr != '+' && *ptr != '-') {
return 0;
}
/* check 1 or 2 digit time zone hour */
pos1 = ++ptr;
ts_skip_number();
if (ptr == pos1 || ptr - pos1 == 3 || ptr - pos1 > 4) {
return 0;
}
if (ptr == end) {
return 1;
}
/* optional time zone minute */
if (*ptr != ':') {
return 0;
}
pos1 = ++ptr;
ts_skip_number();
if (ptr - pos1 != 2) {
return 0;
}
/* skip following space */
ts_skip_space();
return (ptr == end) ? 1 : 0;
}
/* }}} */
/* {{{ eval_sexagesimal_l()
* Convert a base 60 number to a long
*/
static zend_long eval_sexagesimal_l(zend_long lval, const char *sg, const char *eos)
{
const char *ep;
while (sg < eos && (*sg < '0' || *sg > '9')) {
sg++;
}
ep = sg;
while (ep < eos && *ep >= '0' && *ep <= '9') {
ep++;
}
if (sg == eos) {
return lval;
}
return eval_sexagesimal_l(
lval * 60 + ZEND_STRTOL(sg, (char **) NULL, 10), ep, eos);
}
/* }}} */
/* {{{ eval_sexagesimal_d()
* Convert a base 60 number to a double
*/
static double eval_sexagesimal_d(double dval, const char *sg, const char *eos)
{
const char *ep;
while (sg < eos && *sg != '.' && (*sg < '0' || *sg > '9')) {
sg++;
}
ep = sg;
while (ep < eos && *ep >= '0' && *ep <= '9') {
ep++;
}
if (sg == eos || *sg == '.') {
return dval;
}
return eval_sexagesimal_d(
dval * 60.0 + zend_strtod(sg, (const char **) NULL), ep, eos);
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/