From 811ee4f442212c7ee14a9112e4897e2537e40d28 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 20 May 2019 14:10:45 -0700 Subject: Use a custom mktime() function to avoid platform dependencies. --- generated_for_cmake/upb/json/parser.c | 182 +++++++++++++++++++++------------- upb/json/parser.rl | 44 +++++++- 2 files changed, 155 insertions(+), 71 deletions(-) diff --git a/generated_for_cmake/upb/json/parser.c b/generated_for_cmake/upb/json/parser.c index 1e9d2fc..a1bd0d2 100644 --- a/generated_for_cmake/upb/json/parser.c +++ b/generated_for_cmake/upb/json/parser.c @@ -1686,6 +1686,48 @@ static void start_timestamp_zone(upb_json_parser *p, const char *ptr) { capture_begin(p, ptr); } +#define EPOCH_YEAR 1970 +#define TM_YEAR_BASE 1900 + +static bool isleap(int year) { + return (year % 4) == 0 && (year % 100 != 0 || (year % 400) == 0); +} + +const unsigned short int __mon_yday[2][13] = { + /* Normal years. */ + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, + /* Leap years. */ + { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } +}; + +int64_t epoch(int year, int yday, int hour, int min, int sec) { + int64_t years = year - EPOCH_YEAR; + + int64_t leap_days = years / 4 - years / 100 + years / 400; + + int64_t days = years * 365 + yday + leap_days; + int64_t hours = days * 24 + hour; + int64_t mins = hours * 60 + min; + int64_t secs = mins * 60 + sec; + return secs; +} + + +static int64_t upb_mktime(const struct tm *tp) { + int sec = tp->tm_sec; + int min = tp->tm_min; + int hour = tp->tm_hour; + int mday = tp->tm_mday; + int mon = tp->tm_mon; + int year = tp->tm_year + TM_YEAR_BASE; + + /* Calculate day of year from year, month, and day of month. */ + int mon_yday = ((__mon_yday[isleap(year)][mon]) - 1); + int yday = mon_yday + mday; + + return epoch(year, yday, hour, min, sec); +} + static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { size_t len; const char *buf; @@ -1713,7 +1755,7 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { } /* Normalize tm */ - seconds = mktime(&p->tm); + seconds = upb_mktime(&p->tm); /* Check timestamp boundary */ if (seconds < -62135596800) { @@ -2544,11 +2586,11 @@ static bool does_fieldmask_end(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2749 "upb/json/parser.rl" +#line 2791 "upb/json/parser.rl" -#line 2552 "upb/json/parser.c" +#line 2594 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2803,7 +2845,7 @@ static const int json_en_value_machine = 78; static const int json_en_main = 1; -#line 2752 "upb/json/parser.rl" +#line 2794 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2826,7 +2868,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2830 "upb/json/parser.c" +#line 2872 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2901,147 +2943,147 @@ _match: switch ( *_acts++ ) { case 1: -#line 2557 "upb/json/parser.rl" +#line 2599 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2559 "upb/json/parser.rl" +#line 2601 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2563 "upb/json/parser.rl" +#line 2605 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2564 "upb/json/parser.rl" +#line 2606 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2570 "upb/json/parser.rl" +#line 2612 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2571 "upb/json/parser.rl" +#line 2613 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2572 "upb/json/parser.rl" +#line 2614 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2578 "upb/json/parser.rl" +#line 2620 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2584 "upb/json/parser.rl" +#line 2626 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2589 "upb/json/parser.rl" +#line 2631 "upb/json/parser.rl" { start_year(parser, p); } break; case 11: -#line 2590 "upb/json/parser.rl" +#line 2632 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_year(parser, p)); } break; case 12: -#line 2594 "upb/json/parser.rl" +#line 2636 "upb/json/parser.rl" { start_month(parser, p); } break; case 13: -#line 2595 "upb/json/parser.rl" +#line 2637 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_month(parser, p)); } break; case 14: -#line 2599 "upb/json/parser.rl" +#line 2641 "upb/json/parser.rl" { start_day(parser, p); } break; case 15: -#line 2600 "upb/json/parser.rl" +#line 2642 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_day(parser, p)); } break; case 16: -#line 2604 "upb/json/parser.rl" +#line 2646 "upb/json/parser.rl" { start_hour(parser, p); } break; case 17: -#line 2605 "upb/json/parser.rl" +#line 2647 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hour(parser, p)); } break; case 18: -#line 2609 "upb/json/parser.rl" +#line 2651 "upb/json/parser.rl" { start_minute(parser, p); } break; case 19: -#line 2610 "upb/json/parser.rl" +#line 2652 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_minute(parser, p)); } break; case 20: -#line 2614 "upb/json/parser.rl" +#line 2656 "upb/json/parser.rl" { start_second(parser, p); } break; case 21: -#line 2615 "upb/json/parser.rl" +#line 2657 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_second(parser, p)); } break; case 22: -#line 2620 "upb/json/parser.rl" +#line 2662 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 23: -#line 2621 "upb/json/parser.rl" +#line 2663 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 24: -#line 2623 "upb/json/parser.rl" +#line 2665 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 25: -#line 2628 "upb/json/parser.rl" +#line 2670 "upb/json/parser.rl" { start_timestamp_base(parser); } break; case 26: -#line 2630 "upb/json/parser.rl" +#line 2672 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 27: -#line 2631 "upb/json/parser.rl" +#line 2673 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 28: -#line 2633 "upb/json/parser.rl" +#line 2675 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 29: -#line 2634 "upb/json/parser.rl" +#line 2676 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 30: -#line 2636 "upb/json/parser.rl" +#line 2678 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 31: -#line 2641 "upb/json/parser.rl" +#line 2683 "upb/json/parser.rl" { start_fieldmask_path_text(parser, p); } break; case 32: -#line 2642 "upb/json/parser.rl" +#line 2684 "upb/json/parser.rl" { end_fieldmask_path_text(parser, p); } break; case 33: -#line 2647 "upb/json/parser.rl" +#line 2689 "upb/json/parser.rl" { start_fieldmask_path(parser); } break; case 34: -#line 2648 "upb/json/parser.rl" +#line 2690 "upb/json/parser.rl" { end_fieldmask_path(parser); } break; case 35: -#line 2654 "upb/json/parser.rl" +#line 2696 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 36: -#line 2659 "upb/json/parser.rl" +#line 2701 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -3055,11 +3097,11 @@ _match: } break; case 37: -#line 2672 "upb/json/parser.rl" +#line 2714 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 78;goto _again;} } break; case 38: -#line 2677 "upb/json/parser.rl" +#line 2719 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -3069,11 +3111,11 @@ _match: } break; case 39: -#line 2684 "upb/json/parser.rl" +#line 2726 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 40: -#line 2687 "upb/json/parser.rl" +#line 2729 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -3083,7 +3125,7 @@ _match: } break; case 41: -#line 2698 "upb/json/parser.rl" +#line 2740 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -3093,7 +3135,7 @@ _match: } break; case 42: -#line 2707 "upb/json/parser.rl" +#line 2749 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -3103,54 +3145,54 @@ _match: } break; case 43: -#line 2719 "upb/json/parser.rl" +#line 2761 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 44: -#line 2723 "upb/json/parser.rl" +#line 2765 "upb/json/parser.rl" { end_array(parser); } break; case 45: -#line 2728 "upb/json/parser.rl" +#line 2770 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 46: -#line 2729 "upb/json/parser.rl" +#line 2771 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 47: -#line 2731 "upb/json/parser.rl" +#line 2773 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 48: -#line 2732 "upb/json/parser.rl" +#line 2774 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 49: -#line 2734 "upb/json/parser.rl" +#line 2776 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 50: -#line 2736 "upb/json/parser.rl" +#line 2778 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 51: -#line 2738 "upb/json/parser.rl" +#line 2780 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 52: -#line 2740 "upb/json/parser.rl" +#line 2782 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 53: -#line 2741 "upb/json/parser.rl" +#line 2783 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 54: -#line 2746 "upb/json/parser.rl" +#line 2788 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 3154 "upb/json/parser.c" +#line 3196 "upb/json/parser.c" } } @@ -3167,32 +3209,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2555 "upb/json/parser.rl" +#line 2597 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 46: -#line 2729 "upb/json/parser.rl" +#line 2771 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 49: -#line 2734 "upb/json/parser.rl" +#line 2776 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 50: -#line 2736 "upb/json/parser.rl" +#line 2778 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 51: -#line 2738 "upb/json/parser.rl" +#line 2780 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 53: -#line 2741 "upb/json/parser.rl" +#line 2783 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 3196 "upb/json/parser.c" +#line 3238 "upb/json/parser.c" } } } @@ -3200,7 +3242,7 @@ goto _again;} } _out: {} } -#line 2774 "upb/json/parser.rl" +#line 2816 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3243,13 +3285,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3247 "upb/json/parser.c" +#line 3289 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2816 "upb/json/parser.rl" +#line 2858 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 7e260a4..fd3704c 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -1684,6 +1684,48 @@ static void start_timestamp_zone(upb_json_parser *p, const char *ptr) { capture_begin(p, ptr); } +#define EPOCH_YEAR 1970 +#define TM_YEAR_BASE 1900 + +static bool isleap(int year) { + return (year % 4) == 0 && (year % 100 != 0 || (year % 400) == 0); +} + +const unsigned short int __mon_yday[2][13] = { + /* Normal years. */ + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, + /* Leap years. */ + { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } +}; + +int64_t epoch(int year, int yday, int hour, int min, int sec) { + int64_t years = year - EPOCH_YEAR; + + int64_t leap_days = years / 4 - years / 100 + years / 400; + + int64_t days = years * 365 + yday + leap_days; + int64_t hours = days * 24 + hour; + int64_t mins = hours * 60 + min; + int64_t secs = mins * 60 + sec; + return secs; +} + + +static int64_t upb_mktime(const struct tm *tp) { + int sec = tp->tm_sec; + int min = tp->tm_min; + int hour = tp->tm_hour; + int mday = tp->tm_mday; + int mon = tp->tm_mon; + int year = tp->tm_year + TM_YEAR_BASE; + + /* Calculate day of year from year, month, and day of month. */ + int mon_yday = ((__mon_yday[isleap(year)][mon]) - 1); + int yday = mon_yday + mday; + + return epoch(year, yday, hour, min, sec); +} + static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { size_t len; const char *buf; @@ -1711,7 +1753,7 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { } /* Normalize tm */ - seconds = mktime(&p->tm); + seconds = upb_mktime(&p->tm); /* Check timestamp boundary */ if (seconds < -62135596800) { -- cgit v1.2.3