From 99779b455ce3bf9c53dd411575766bf298a3a5f3 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 14 Dec 2021 15:59:07 +0100 Subject: introducing GNUNET_TIME_Timestamp --- src/json/json_helper.c | 124 ++++++++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 64 deletions(-) (limited to 'src/json/json_helper.c') 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 () * @param[out] spec where to write the data * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error */ -static int +static enum GNUNET_GenericReturnValue parse_fixed_data (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) @@ -579,20 +579,20 @@ GNUNET_JSON_spec_int64 (const char *name, /* ************ GNUnet-specific parser specifications ******************* */ /** - * Parse given JSON object to absolute time. + * Parse given JSON object to a timestamp. * * @param cls closure, NULL * @param root the json object representing data * @param[out] spec where to write the data * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error */ -static int -parse_abs_time (void *cls, - json_t *root, - struct GNUNET_JSON_Specification *spec) +static enum GNUNET_GenericReturnValue +parse_timestamp (void *cls, + json_t *root, + struct GNUNET_JSON_Specification *spec) { - struct GNUNET_TIME_Absolute *abs = spec->ptr; - json_t *json_t_ms; + struct GNUNET_TIME_Timestamp *ts = spec->ptr; + json_t *json_t_s; unsigned long long int tval; if (! json_is_object (root)) @@ -600,14 +600,16 @@ parse_abs_time (void *cls, GNUNET_break_op (0); return GNUNET_SYSERR; } - json_t_ms = json_object_get (root, "t_ms"); - if (json_is_integer (json_t_ms)) + json_t_s = json_object_get (root, + "t_s"); + if (json_is_integer (json_t_s)) { - tval = json_integer_value (json_t_ms); - /* Time is in milliseconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ - abs->abs_value_us = tval * GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us; - if ((abs->abs_value_us) - / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us + tval = json_integer_value (json_t_s); + /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ + ts->abs_time.abs_value_us + = tval * GNUNET_TIME_UNIT_SECONDS.rel_value_us; + if (ts->abs_time.abs_value_us + / GNUNET_TIME_UNIT_SECONDS.rel_value_us != tval) { /* Integer overflow */ @@ -616,14 +618,15 @@ parse_abs_time (void *cls, } return GNUNET_OK; } - if (json_is_string (json_t_ms)) + if (json_is_string (json_t_s)) { const char *val; - val = json_string_value (json_t_ms); - if ((0 == strcasecmp (val, "never"))) + val = json_string_value (json_t_s); + if ((0 == strcasecmp (val, + "never"))) { - *abs = GNUNET_TIME_UNIT_FOREVER_ABS; + ts->abs_time = GNUNET_TIME_UNIT_FOREVER_ABS; return GNUNET_OK; } GNUNET_break_op (0); @@ -635,17 +638,14 @@ parse_abs_time (void *cls, struct GNUNET_JSON_Specification -GNUNET_JSON_spec_absolute_time (const char *name, - struct GNUNET_TIME_Absolute *at) +GNUNET_JSON_spec_timestamp (const char *name, + struct GNUNET_TIME_Timestamp *t) { struct GNUNET_JSON_Specification ret = { - .parser = &parse_abs_time, - .cleaner = NULL, - .cls = NULL, + .parser = &parse_timestamp, .field = name, - .ptr = at, - .ptr_size = sizeof(struct GNUNET_TIME_Absolute), - .size_ptr = NULL + .ptr = t, + .ptr_size = sizeof(struct GNUNET_TIME_Timestamp) }; return ret; @@ -660,40 +660,37 @@ GNUNET_JSON_spec_absolute_time (const char *name, * @param[out] spec where to write the data * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error */ -static int -parse_abs_time_nbo (void *cls, - json_t *root, - struct GNUNET_JSON_Specification *spec) +static enum GNUNET_GenericReturnValue +parse_timestamp_nbo (void *cls, + json_t *root, + struct GNUNET_JSON_Specification *spec) { - struct GNUNET_TIME_AbsoluteNBO *abs = spec->ptr; - struct GNUNET_TIME_Absolute a; + struct GNUNET_TIME_TimestampNBO *ts = spec->ptr; + struct GNUNET_TIME_Timestamp a; struct GNUNET_JSON_Specification ispec; ispec = *spec; - ispec.parser = &parse_abs_time; + ispec.parser = &parse_timestamp; ispec.ptr = &a; if (GNUNET_OK != - parse_abs_time (NULL, - root, - &ispec)) + parse_timestamp (NULL, + root, + &ispec)) return GNUNET_SYSERR; - *abs = GNUNET_TIME_absolute_hton (a); + *ts = GNUNET_TIME_timestamp_hton (a); return GNUNET_OK; } struct GNUNET_JSON_Specification -GNUNET_JSON_spec_absolute_time_nbo (const char *name, - struct GNUNET_TIME_AbsoluteNBO *at) +GNUNET_JSON_spec_timestamp_nbo (const char *name, + struct GNUNET_TIME_TimestampNBO *at) { struct GNUNET_JSON_Specification ret = { - .parser = &parse_abs_time_nbo, - .cleaner = NULL, - .cls = NULL, + .parser = &parse_timestamp_nbo, .field = name, .ptr = at, - .ptr_size = sizeof(struct GNUNET_TIME_AbsoluteNBO), - .size_ptr = NULL + .ptr_size = sizeof(struct GNUNET_TIME_TimestampNBO) }; return ret; @@ -708,13 +705,13 @@ GNUNET_JSON_spec_absolute_time_nbo (const char *name, * @param[out] spec where to write the data * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error */ -static int +static enum GNUNET_GenericReturnValue parse_rel_time (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) { struct GNUNET_TIME_Relative *rel = spec->ptr; - json_t *json_d_ms; + json_t *json_d_us; unsigned long long int tval; if (! json_is_object (root)) @@ -722,25 +719,27 @@ parse_rel_time (void *cls, GNUNET_break_op (0); return GNUNET_SYSERR; } - json_d_ms = json_object_get (root, "d_ms"); - if (json_is_integer (json_d_ms)) + json_d_us = json_object_get (root, + "d_us"); + if (json_is_integer (json_d_us)) { - tval = json_integer_value (json_d_ms); - /* Time is in milliseconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ - rel->rel_value_us = tval * 1000LL; - if ((rel->rel_value_us) / 1000LL != tval) + tval = json_integer_value (json_d_us); + if (tval >= (1LLU << 53)) { - /* Integer overflow */ + /* value is larger than allowed */ GNUNET_break_op (0); return GNUNET_SYSERR; } + rel->rel_value_us = tval; return GNUNET_OK; } - if (json_is_string (json_d_ms)) + if (json_is_string (json_d_us)) { const char *val; - val = json_string_value (json_d_ms); - if ((0 == strcasecmp (val, "forever"))) + + val = json_string_value (json_d_us); + if ((0 == strcasecmp (val, + "forever"))) { *rel = GNUNET_TIME_UNIT_FOREVER_REL; return GNUNET_OK; @@ -759,12 +758,9 @@ GNUNET_JSON_spec_relative_time (const char *name, { struct GNUNET_JSON_Specification ret = { .parser = &parse_rel_time, - .cleaner = NULL, - .cls = NULL, .field = name, .ptr = rt, - .ptr_size = sizeof(struct GNUNET_TIME_Relative), - .size_ptr = NULL + .ptr_size = sizeof(struct GNUNET_TIME_Relative) }; return ret; @@ -779,7 +775,7 @@ GNUNET_JSON_spec_relative_time (const char *name, * @param[out] spec where to write the data * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error */ -static int +static enum GNUNET_GenericReturnValue parse_rsa_public_key (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) @@ -864,7 +860,7 @@ GNUNET_JSON_spec_rsa_public_key (const char *name, * @param[out] spec where to write the data * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error */ -static int +static enum GNUNET_GenericReturnValue parse_rsa_signature (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) @@ -952,7 +948,7 @@ GNUNET_JSON_spec_rsa_signature (const char *name, * @param[out] spec where to write the data * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error */ -static int +static enum GNUNET_GenericReturnValue parse_boolean (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) -- cgit v1.2.3