aboutsummaryrefslogtreecommitdiff
path: root/src/json/json_helper.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-12-14 15:59:07 +0100
committerChristian Grothoff <christian@grothoff.org>2021-12-14 15:59:12 +0100
commit99779b455ce3bf9c53dd411575766bf298a3a5f3 (patch)
tree12cd49f097fdd1947580b08ec10ca961f8c250b9 /src/json/json_helper.c
parent8c702327dae9e504e0f6e1678884d9327321f44a (diff)
downloadgnunet-99779b455ce3bf9c53dd411575766bf298a3a5f3.tar.gz
gnunet-99779b455ce3bf9c53dd411575766bf298a3a5f3.zip
introducing GNUNET_TIME_Timestamp
Diffstat (limited to 'src/json/json_helper.c')
-rw-r--r--src/json/json_helper.c124
1 files changed, 60 insertions, 64 deletions
diff --git a/src/json/json_helper.c b/src/json/json_helper.c
index 3a11f205c..73f5fc00c 100644
--- a/src/json/json_helper.c
+++ b/src/json/json_helper.c
@@ -49,7 +49,7 @@ GNUNET_JSON_spec_end ()
49 * @param[out] spec where to write the data 49 * @param[out] spec where to write the data
50 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 50 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
51 */ 51 */
52static int 52static enum GNUNET_GenericReturnValue
53parse_fixed_data (void *cls, 53parse_fixed_data (void *cls,
54 json_t *root, 54 json_t *root,
55 struct GNUNET_JSON_Specification *spec) 55 struct GNUNET_JSON_Specification *spec)
@@ -579,20 +579,20 @@ GNUNET_JSON_spec_int64 (const char *name,
579/* ************ GNUnet-specific parser specifications ******************* */ 579/* ************ GNUnet-specific parser specifications ******************* */
580 580
581/** 581/**
582 * Parse given JSON object to absolute time. 582 * Parse given JSON object to a timestamp.
583 * 583 *
584 * @param cls closure, NULL 584 * @param cls closure, NULL
585 * @param root the json object representing data 585 * @param root the json object representing data
586 * @param[out] spec where to write the data 586 * @param[out] spec where to write the data
587 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 587 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
588 */ 588 */
589static int 589static enum GNUNET_GenericReturnValue
590parse_abs_time (void *cls, 590parse_timestamp (void *cls,
591 json_t *root, 591 json_t *root,
592 struct GNUNET_JSON_Specification *spec) 592 struct GNUNET_JSON_Specification *spec)
593{ 593{
594 struct GNUNET_TIME_Absolute *abs = spec->ptr; 594 struct GNUNET_TIME_Timestamp *ts = spec->ptr;
595 json_t *json_t_ms; 595 json_t *json_t_s;
596 unsigned long long int tval; 596 unsigned long long int tval;
597 597
598 if (! json_is_object (root)) 598 if (! json_is_object (root))
@@ -600,14 +600,16 @@ parse_abs_time (void *cls,
600 GNUNET_break_op (0); 600 GNUNET_break_op (0);
601 return GNUNET_SYSERR; 601 return GNUNET_SYSERR;
602 } 602 }
603 json_t_ms = json_object_get (root, "t_ms"); 603 json_t_s = json_object_get (root,
604 if (json_is_integer (json_t_ms)) 604 "t_s");
605 if (json_is_integer (json_t_s))
605 { 606 {
606 tval = json_integer_value (json_t_ms); 607 tval = json_integer_value (json_t_s);
607 /* Time is in milliseconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ 608 /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */
608 abs->abs_value_us = tval * GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us; 609 ts->abs_time.abs_value_us
609 if ((abs->abs_value_us) 610 = tval * GNUNET_TIME_UNIT_SECONDS.rel_value_us;
610 / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us 611 if (ts->abs_time.abs_value_us
612 / GNUNET_TIME_UNIT_SECONDS.rel_value_us
611 != tval) 613 != tval)
612 { 614 {
613 /* Integer overflow */ 615 /* Integer overflow */
@@ -616,14 +618,15 @@ parse_abs_time (void *cls,
616 } 618 }
617 return GNUNET_OK; 619 return GNUNET_OK;
618 } 620 }
619 if (json_is_string (json_t_ms)) 621 if (json_is_string (json_t_s))
620 { 622 {
621 const char *val; 623 const char *val;
622 624
623 val = json_string_value (json_t_ms); 625 val = json_string_value (json_t_s);
624 if ((0 == strcasecmp (val, "never"))) 626 if ((0 == strcasecmp (val,
627 "never")))
625 { 628 {
626 *abs = GNUNET_TIME_UNIT_FOREVER_ABS; 629 ts->abs_time = GNUNET_TIME_UNIT_FOREVER_ABS;
627 return GNUNET_OK; 630 return GNUNET_OK;
628 } 631 }
629 GNUNET_break_op (0); 632 GNUNET_break_op (0);
@@ -635,17 +638,14 @@ parse_abs_time (void *cls,
635 638
636 639
637struct GNUNET_JSON_Specification 640struct GNUNET_JSON_Specification
638GNUNET_JSON_spec_absolute_time (const char *name, 641GNUNET_JSON_spec_timestamp (const char *name,
639 struct GNUNET_TIME_Absolute *at) 642 struct GNUNET_TIME_Timestamp *t)
640{ 643{
641 struct GNUNET_JSON_Specification ret = { 644 struct GNUNET_JSON_Specification ret = {
642 .parser = &parse_abs_time, 645 .parser = &parse_timestamp,
643 .cleaner = NULL,
644 .cls = NULL,
645 .field = name, 646 .field = name,
646 .ptr = at, 647 .ptr = t,
647 .ptr_size = sizeof(struct GNUNET_TIME_Absolute), 648 .ptr_size = sizeof(struct GNUNET_TIME_Timestamp)
648 .size_ptr = NULL
649 }; 649 };
650 650
651 return ret; 651 return ret;
@@ -660,40 +660,37 @@ GNUNET_JSON_spec_absolute_time (const char *name,
660 * @param[out] spec where to write the data 660 * @param[out] spec where to write the data
661 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 661 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
662 */ 662 */
663static int 663static enum GNUNET_GenericReturnValue
664parse_abs_time_nbo (void *cls, 664parse_timestamp_nbo (void *cls,
665 json_t *root, 665 json_t *root,
666 struct GNUNET_JSON_Specification *spec) 666 struct GNUNET_JSON_Specification *spec)
667{ 667{
668 struct GNUNET_TIME_AbsoluteNBO *abs = spec->ptr; 668 struct GNUNET_TIME_TimestampNBO *ts = spec->ptr;
669 struct GNUNET_TIME_Absolute a; 669 struct GNUNET_TIME_Timestamp a;
670 struct GNUNET_JSON_Specification ispec; 670 struct GNUNET_JSON_Specification ispec;
671 671
672 ispec = *spec; 672 ispec = *spec;
673 ispec.parser = &parse_abs_time; 673 ispec.parser = &parse_timestamp;
674 ispec.ptr = &a; 674 ispec.ptr = &a;
675 if (GNUNET_OK != 675 if (GNUNET_OK !=
676 parse_abs_time (NULL, 676 parse_timestamp (NULL,
677 root, 677 root,
678 &ispec)) 678 &ispec))
679 return GNUNET_SYSERR; 679 return GNUNET_SYSERR;
680 *abs = GNUNET_TIME_absolute_hton (a); 680 *ts = GNUNET_TIME_timestamp_hton (a);
681 return GNUNET_OK; 681 return GNUNET_OK;
682} 682}
683 683
684 684
685struct GNUNET_JSON_Specification 685struct GNUNET_JSON_Specification
686GNUNET_JSON_spec_absolute_time_nbo (const char *name, 686GNUNET_JSON_spec_timestamp_nbo (const char *name,
687 struct GNUNET_TIME_AbsoluteNBO *at) 687 struct GNUNET_TIME_TimestampNBO *at)
688{ 688{
689 struct GNUNET_JSON_Specification ret = { 689 struct GNUNET_JSON_Specification ret = {
690 .parser = &parse_abs_time_nbo, 690 .parser = &parse_timestamp_nbo,
691 .cleaner = NULL,
692 .cls = NULL,
693 .field = name, 691 .field = name,
694 .ptr = at, 692 .ptr = at,
695 .ptr_size = sizeof(struct GNUNET_TIME_AbsoluteNBO), 693 .ptr_size = sizeof(struct GNUNET_TIME_TimestampNBO)
696 .size_ptr = NULL
697 }; 694 };
698 695
699 return ret; 696 return ret;
@@ -708,13 +705,13 @@ GNUNET_JSON_spec_absolute_time_nbo (const char *name,
708 * @param[out] spec where to write the data 705 * @param[out] spec where to write the data
709 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 706 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
710 */ 707 */
711static int 708static enum GNUNET_GenericReturnValue
712parse_rel_time (void *cls, 709parse_rel_time (void *cls,
713 json_t *root, 710 json_t *root,
714 struct GNUNET_JSON_Specification *spec) 711 struct GNUNET_JSON_Specification *spec)
715{ 712{
716 struct GNUNET_TIME_Relative *rel = spec->ptr; 713 struct GNUNET_TIME_Relative *rel = spec->ptr;
717 json_t *json_d_ms; 714 json_t *json_d_us;
718 unsigned long long int tval; 715 unsigned long long int tval;
719 716
720 if (! json_is_object (root)) 717 if (! json_is_object (root))
@@ -722,25 +719,27 @@ parse_rel_time (void *cls,
722 GNUNET_break_op (0); 719 GNUNET_break_op (0);
723 return GNUNET_SYSERR; 720 return GNUNET_SYSERR;
724 } 721 }
725 json_d_ms = json_object_get (root, "d_ms"); 722 json_d_us = json_object_get (root,
726 if (json_is_integer (json_d_ms)) 723 "d_us");
724 if (json_is_integer (json_d_us))
727 { 725 {
728 tval = json_integer_value (json_d_ms); 726 tval = json_integer_value (json_d_us);
729 /* Time is in milliseconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ 727 if (tval >= (1LLU << 53))
730 rel->rel_value_us = tval * 1000LL;
731 if ((rel->rel_value_us) / 1000LL != tval)
732 { 728 {
733 /* Integer overflow */ 729 /* value is larger than allowed */
734 GNUNET_break_op (0); 730 GNUNET_break_op (0);
735 return GNUNET_SYSERR; 731 return GNUNET_SYSERR;
736 } 732 }
733 rel->rel_value_us = tval;
737 return GNUNET_OK; 734 return GNUNET_OK;
738 } 735 }
739 if (json_is_string (json_d_ms)) 736 if (json_is_string (json_d_us))
740 { 737 {
741 const char *val; 738 const char *val;
742 val = json_string_value (json_d_ms); 739
743 if ((0 == strcasecmp (val, "forever"))) 740 val = json_string_value (json_d_us);
741 if ((0 == strcasecmp (val,
742 "forever")))
744 { 743 {
745 *rel = GNUNET_TIME_UNIT_FOREVER_REL; 744 *rel = GNUNET_TIME_UNIT_FOREVER_REL;
746 return GNUNET_OK; 745 return GNUNET_OK;
@@ -759,12 +758,9 @@ GNUNET_JSON_spec_relative_time (const char *name,
759{ 758{
760 struct GNUNET_JSON_Specification ret = { 759 struct GNUNET_JSON_Specification ret = {
761 .parser = &parse_rel_time, 760 .parser = &parse_rel_time,
762 .cleaner = NULL,
763 .cls = NULL,
764 .field = name, 761 .field = name,
765 .ptr = rt, 762 .ptr = rt,
766 .ptr_size = sizeof(struct GNUNET_TIME_Relative), 763 .ptr_size = sizeof(struct GNUNET_TIME_Relative)
767 .size_ptr = NULL
768 }; 764 };
769 765
770 return ret; 766 return ret;
@@ -779,7 +775,7 @@ GNUNET_JSON_spec_relative_time (const char *name,
779 * @param[out] spec where to write the data 775 * @param[out] spec where to write the data
780 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 776 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
781 */ 777 */
782static int 778static enum GNUNET_GenericReturnValue
783parse_rsa_public_key (void *cls, 779parse_rsa_public_key (void *cls,
784 json_t *root, 780 json_t *root,
785 struct GNUNET_JSON_Specification *spec) 781 struct GNUNET_JSON_Specification *spec)
@@ -864,7 +860,7 @@ GNUNET_JSON_spec_rsa_public_key (const char *name,
864 * @param[out] spec where to write the data 860 * @param[out] spec where to write the data
865 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 861 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
866 */ 862 */
867static int 863static enum GNUNET_GenericReturnValue
868parse_rsa_signature (void *cls, 864parse_rsa_signature (void *cls,
869 json_t *root, 865 json_t *root,
870 struct GNUNET_JSON_Specification *spec) 866 struct GNUNET_JSON_Specification *spec)
@@ -952,7 +948,7 @@ GNUNET_JSON_spec_rsa_signature (const char *name,
952 * @param[out] spec where to write the data 948 * @param[out] spec where to write the data
953 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 949 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
954 */ 950 */
955static int 951static enum GNUNET_GenericReturnValue
956parse_boolean (void *cls, 952parse_boolean (void *cls,
957 json_t *root, 953 json_t *root,
958 struct GNUNET_JSON_Specification *spec) 954 struct GNUNET_JSON_Specification *spec)