GNUnet  0.17.6
Functions
json_helper.c File Reference

functions to generate specifciations for JSON parsing More...

#include "platform.h"
#include "gnunet_json_lib.h"
Include dependency graph for json_helper.c:

Go to the source code of this file.

Functions

struct GNUNET_JSON_Specification GNUNET_JSON_spec_end ()
 End of a parser specification. More...
 
static enum GNUNET_GenericReturnValue parse_fixed_data (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to fixed size data. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_fixed (const char *name, void *obj, size_t size)
 Variable size object (in network byte order, encoded using Crockford Base32hex encoding). More...
 
static enum GNUNET_GenericReturnValue parse_fixed64_data (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to fixed size data. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_fixed64 (const char *name, void *obj, size_t size)
 Variable size object (in network byte order, encoded using base64 encoding). More...
 
static enum GNUNET_GenericReturnValue parse_variable_data (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to variable size data. More...
 
static void clean_variable_data (void *cls, struct GNUNET_JSON_Specification *spec)
 Cleanup data left from parsing variable size data. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_varsize (const char *name, void **obj, size_t *size)
 Variable size object (in network byte order, encoded using Crockford Base32hex encoding). More...
 
static enum GNUNET_GenericReturnValue parse_string (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to string. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_string (const char *name, const char **strptr)
 The expected field stores a string. More...
 
static enum GNUNET_GenericReturnValue parse_object (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to a JSON object. More...
 
static void clean_object (void *cls, struct GNUNET_JSON_Specification *spec)
 Cleanup data left from parsing JSON object. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_json (const char *name, json_t **jsonp)
 JSON object. More...
 
static enum GNUNET_GenericReturnValue parse_bool (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to a bool. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_bool (const char *name, bool *b)
 boolean. More...
 
static enum GNUNET_GenericReturnValue parse_u8 (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to a uint8_t. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint8 (const char *name, uint8_t *u8)
 8-bit integer. More...
 
static enum GNUNET_GenericReturnValue parse_u16 (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to a uint16_t. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint16 (const char *name, uint16_t *u16)
 16-bit integer. More...
 
static enum GNUNET_GenericReturnValue parse_u32 (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to a uint32_t. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint32 (const char *name, uint32_t *u32)
 32-bit integer. More...
 
static enum GNUNET_GenericReturnValue parse_u64 (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to a uint64_t. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint64 (const char *name, uint64_t *u64)
 64-bit integer. More...
 
static enum GNUNET_GenericReturnValue parse_i64 (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to a int64_t. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_int64 (const char *name, int64_t *i64)
 64-bit signed integer. More...
 
static enum GNUNET_GenericReturnValue parse_timestamp (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to a timestamp. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_timestamp (const char *name, struct GNUNET_TIME_Timestamp *t)
 Timestamp. More...
 
static enum GNUNET_GenericReturnValue parse_timestamp_nbo (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to absolute time. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_timestamp_nbo (const char *name, struct GNUNET_TIME_TimestampNBO *at)
 Timestamp in network byte order. More...
 
static enum GNUNET_GenericReturnValue parse_rel_time (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to relative time. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_relative_time (const char *name, struct GNUNET_TIME_Relative *rt)
 Relative time. More...
 
static enum GNUNET_GenericReturnValue parse_rsa_public_key (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to RSA public key. More...
 
static void clean_rsa_public_key (void *cls, struct GNUNET_JSON_Specification *spec)
 Cleanup data left from parsing RSA public key. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_public_key (const char *name, struct GNUNET_CRYPTO_RsaPublicKey **pk)
 Specification for parsing an RSA public key. More...
 
static enum GNUNET_GenericReturnValue parse_rsa_signature (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to RSA signature. More...
 
static void clean_rsa_signature (void *cls, struct GNUNET_JSON_Specification *spec)
 Cleanup data left from parsing RSA signature. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_signature (const char *name, struct GNUNET_CRYPTO_RsaSignature **sig)
 Specification for parsing an RSA signature. More...
 
static enum GNUNET_GenericReturnValue parse_boolean (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Parse given JSON object to an int as a boolean. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_boolean (const char *name, int *boolean)
 Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO). More...
 

Detailed Description

functions to generate specifciations for JSON parsing

Author
Florian Dold
Benedikt Mueller
Christian Grothoff

Definition in file json_helper.c.

Function Documentation

◆ GNUNET_JSON_spec_end()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_end ( void  )

End of a parser specification.

Definition at line 1 of file json_helper.c.

33 {
35  .parser = NULL,
36  .cleaner = NULL,
37  .cls = NULL
38  };
39 
40  return ret;
41 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().

References ret.

Referenced by add_attribute_cont(), add_credential_cont(), consume_ticket_cont(), namestore_add_or_update(), and revoke_ticket_cont().

Here is the caller graph for this function:

◆ parse_fixed_data()

static enum GNUNET_GenericReturnValue parse_fixed_data ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to fixed size data.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 1 of file json_helper.c.

56 {
57  const char *enc;
58  unsigned int len;
59 
60  if (NULL == (enc = json_string_value (root)))
61  {
62  GNUNET_break_op (0);
63  return GNUNET_SYSERR;
64  }
65  len = strlen (enc);
66  if (((len * 5) / 8) != spec->ptr_size)
67  {
68  GNUNET_break_op (0);
70  "Field `%s' has wrong length\n",
71  spec->field);
72  return GNUNET_SYSERR;
73  }
74  if (GNUNET_OK !=
76  len,
77  spec->ptr,
78  spec->ptr_size))
79  {
80  GNUNET_break_op (0);
81  return GNUNET_SYSERR;
82  }
83  return GNUNET_OK;
84 }
static OpusEncoder * enc
OPUS encoder.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
#define GNUNET_log(kind,...)
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_SYSERR
Definition: gnunet_common.h:97
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
@ GNUNET_ERROR_TYPE_WARNING
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition: strings.c:789
void * ptr
Pointer, details specific to the parser.
const char * field
Name of the field to parse, use NULL to get the JSON of the main object instead of the JSON of an ind...
size_t ptr_size
Number of bytes available in ptr.

◆ GNUNET_JSON_spec_fixed()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_fixed ( const char *  name,
void *  obj,
size_t  size 
)

Variable size object (in network byte order, encoded using Crockford Base32hex encoding).

Parameters
namename of the JSON field
[out]objpointer where to write the data, must have size bytes
sizenumber of bytes expected in obj

Definition at line 1 of file json_helper.c.

91 {
93  .parser = &parse_fixed_data,
94  .cleaner = NULL,
95  .cls = NULL,
96  .field = name,
97  .ptr = obj,
98  .ptr_size = size,
99  .size_ptr = NULL
100  };
101 
102  return ret;
103 }
static enum GNUNET_GenericReturnValue parse_fixed_data(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to fixed size data.
Definition: json_helper.c:53
static unsigned int size
Size of the "table".
Definition: peer.c:67
const char * name

◆ parse_fixed64_data()

static enum GNUNET_GenericReturnValue parse_fixed64_data ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to fixed size data.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 1 of file json_helper.c.

118 {
119  const char *enc;
120  unsigned int len;
121  void *output;
122  size_t olen;
123 
124  if (NULL == (enc = json_string_value (root)))
125  {
126  GNUNET_break_op (0);
127  return GNUNET_SYSERR;
128  }
129  len = strlen (enc);
130  output = NULL;
132  len,
133  &output);
134  if (olen != spec->ptr_size)
135  {
136  GNUNET_break_op (0);
138  "Field `%s' has wrong length\n",
139  spec->field);
140  GNUNET_free (output);
141  return GNUNET_SYSERR;
142  }
143  memcpy (spec->ptr,
144  output,
145  olen);
146  GNUNET_free (output);
147  return GNUNET_OK;
148 }
#define GNUNET_free(ptr)
Wrapper around free.
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **output)
Decode from Base64.
Definition: strings.c:1696

◆ GNUNET_JSON_spec_fixed64()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_fixed64 ( const char *  name,
void *  obj,
size_t  size 
)

Variable size object (in network byte order, encoded using base64 encoding).

Parameters
namename of the JSON field
[out]objpointer where to write the data, must have size bytes
sizenumber of bytes expected in obj

Definition at line 1 of file json_helper.c.

155 {
156  struct GNUNET_JSON_Specification ret = {
157  .parser = &parse_fixed64_data,
158  .cleaner = NULL,
159  .cls = NULL,
160  .field = name,
161  .ptr = obj,
162  .ptr_size = size,
163  .size_ptr = NULL
164  };
165 
166  return ret;
167 }
static enum GNUNET_GenericReturnValue parse_fixed64_data(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to fixed size data.
Definition: json_helper.c:115

◆ parse_variable_data()

static enum GNUNET_GenericReturnValue parse_variable_data ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to variable size data.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 1 of file json_helper.c.

182 {
183  const char *str;
184  size_t size;
185  void *data;
186 
187  str = json_string_value (root);
188  if (NULL == str)
189  {
190  GNUNET_break_op (0);
191  return GNUNET_SYSERR;
192  }
193  if (GNUNET_OK !=
195  strlen (str),
196  &data,
197  &size))
198  {
199  GNUNET_break_op (0);
200  return GNUNET_SYSERR;
201  }
202  *(void **) spec->ptr = data;
203  *spec->size_ptr = size;
204  return GNUNET_OK;
205 }
uint32_t data
The data value.
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data_alloc(const char *enc, size_t enclen, void **out, size_t *out_size)
Convert CrockfordBase32 encoding back to data.
Definition: strings.c:855
size_t * size_ptr
Where should we store the final size of ptr.

◆ clean_variable_data()

static void clean_variable_data ( void *  cls,
struct GNUNET_JSON_Specification spec 
)
static

Cleanup data left from parsing variable size data.

Parameters
clsclosure, NULL
[out]specwhere to free the data

Definition at line 215 of file json_helper.c.

217 {
218  (void) cls;
219  if (0 != *spec->size_ptr)
220  {
221  GNUNET_free (*(void **) spec->ptr);
222  *(void **) spec->ptr = NULL;
223  *spec->size_ptr = 0;
224  }
225 }

References GNUNET_JSON_Specification::cls, GNUNET_free, GNUNET_JSON_Specification::ptr, and GNUNET_JSON_Specification::size_ptr.

◆ GNUNET_JSON_spec_varsize()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_varsize ( const char *  name,
void **  obj,
size_t *  size 
)

Variable size object (in network byte order, encoded using Crockford Base32hex encoding).

Parameters
namename of the JSON field
[out]objpointer where to write the data, will be allocated
[out]sizewhere to store the number of bytes allocated for obj

Definition at line 215 of file json_helper.c.

232 {
233  struct GNUNET_JSON_Specification ret = {
234  .parser = &parse_variable_data,
235  .cleaner = &clean_variable_data,
236  .cls = NULL,
237  .field = name,
238  .ptr = obj,
239  .ptr_size = 0,
240  .size_ptr = size
241  };
242 
243  *obj = NULL;
244  *size = 0;
245  return ret;
246 }
static void clean_variable_data(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing variable size data.
Definition: json_helper.c:215
static enum GNUNET_GenericReturnValue parse_variable_data(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to variable size data.
Definition: json_helper.c:179

◆ parse_string()

static enum GNUNET_GenericReturnValue parse_string ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to string.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 215 of file json_helper.c.

261 {
262  const char *str;
263 
264  (void) cls;
265  str = json_string_value (root);
266  if (NULL == str)
267  {
268  GNUNET_break_op (0);
269  return GNUNET_SYSERR;
270  }
271  *(const char **) spec->ptr = str;
272  return GNUNET_OK;
273 }

◆ GNUNET_JSON_spec_string()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_string ( const char *  name,
const char **  strptr 
)

The expected field stores a string.

Parameters
namename of the JSON field
strptrwhere to store a pointer to the field

Definition at line 215 of file json_helper.c.

279 {
280  struct GNUNET_JSON_Specification ret = {
281  .parser = &parse_string,
282  .cleaner = NULL,
283  .cls = NULL,
284  .field = name,
285  .ptr = strptr,
286  .ptr_size = 0,
287  .size_ptr = NULL
288  };
289 
290  *strptr = NULL;
291  return ret;
292 }
static enum GNUNET_GenericReturnValue parse_string(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to string.
Definition: json_helper.c:258

◆ parse_object()

static enum GNUNET_GenericReturnValue parse_object ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to a JSON object.

(Yes, trivial.)

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 215 of file json_helper.c.

307 {
308  if (! (json_is_object (root) || json_is_array (root)))
309  {
310  GNUNET_break_op (0);
311  return GNUNET_SYSERR;
312  }
313  json_incref (root);
314  *(json_t **) spec->ptr = root;
315  return GNUNET_OK;
316 }

◆ clean_object()

static void clean_object ( void *  cls,
struct GNUNET_JSON_Specification spec 
)
static

Cleanup data left from parsing JSON object.

Parameters
clsclosure, NULL
[out]specwhere to free the data

Definition at line 326 of file json_helper.c.

328 {
329  json_t **ptr = (json_t **) spec->ptr;
330 
331  if (NULL != *ptr)
332  {
333  json_decref (*ptr);
334  *ptr = NULL;
335  }
336 }

References GNUNET_JSON_Specification::ptr.

◆ GNUNET_JSON_spec_json()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_json ( const char *  name,
json_t **  jsonp 
)

JSON object.

Parameters
namename of the JSON field
[out]jsonpwhere to store the JSON found under name

Definition at line 326 of file json_helper.c.

342 {
343  struct GNUNET_JSON_Specification ret = {
344  .parser = &parse_object,
345  .cleaner = &clean_object,
346  .cls = NULL,
347  .field = name,
348  .ptr = jsonp,
349  .ptr_size = 0,
350  .size_ptr = NULL
351  };
352 
353  *jsonp = NULL;
354  return ret;
355 }
static void clean_object(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing JSON object.
Definition: json_helper.c:326
static enum GNUNET_GenericReturnValue parse_object(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a JSON object.
Definition: json_helper.c:304

◆ parse_bool()

static enum GNUNET_GenericReturnValue parse_bool ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to a bool.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 326 of file json_helper.c.

370 {
371  bool *b = spec->ptr;
372 
373  if (json_true () == root)
374  {
375  *b = true;
376  return GNUNET_OK;
377  }
378  if (json_false () == root)
379  {
380  *b = false;
381  return GNUNET_OK;
382  }
383  GNUNET_break_op (0);
384  return GNUNET_SYSERR;
385 }

◆ GNUNET_JSON_spec_bool()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_bool ( const char *  name,
bool *  b 
)

boolean.

Parameters
namename of the JSON field
[out]bwhere to store the boolean found under name

Definition at line 326 of file json_helper.c.

391 {
392  struct GNUNET_JSON_Specification ret = {
393  .parser = &parse_bool,
394  .cleaner = NULL,
395  .cls = NULL,
396  .field = name,
397  .ptr = b,
398  .ptr_size = sizeof(bool),
399  .size_ptr = NULL
400  };
401 
402  return ret;
403 }
static enum GNUNET_GenericReturnValue parse_bool(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a bool.
Definition: json_helper.c:367

◆ parse_u8()

static enum GNUNET_GenericReturnValue parse_u8 ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to a uint8_t.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 326 of file json_helper.c.

418 {
419  json_int_t val;
420  uint8_t *up = spec->ptr;
421 
422  if (! json_is_integer (root))
423  {
424  GNUNET_break_op (0);
425  return GNUNET_SYSERR;
426  }
427  val = json_integer_value (root);
428  if ((0 > val) || (val > UINT8_MAX))
429  {
430  GNUNET_break_op (0);
431  return GNUNET_SYSERR;
432  }
433  *up = (uint8_t) val;
434  return GNUNET_OK;
435 }

◆ GNUNET_JSON_spec_uint8()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint8 ( const char *  name,
uint8_t *  u8 
)

8-bit integer.

Parameters
namename of the JSON field
[out]u8where to store the integer found under name

Definition at line 326 of file json_helper.c.

441 {
442  struct GNUNET_JSON_Specification ret = {
443  .parser = &parse_u8,
444  .cleaner = NULL,
445  .cls = NULL,
446  .field = name,
447  .ptr = u8,
448  .ptr_size = sizeof(uint8_t),
449  .size_ptr = NULL
450  };
451 
452  return ret;
453 }
static enum GNUNET_GenericReturnValue parse_u8(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint8_t.
Definition: json_helper.c:415

◆ parse_u16()

static enum GNUNET_GenericReturnValue parse_u16 ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to a uint16_t.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 326 of file json_helper.c.

468 {
469  json_int_t val;
470  uint16_t *up = spec->ptr;
471 
472  if (! json_is_integer (root))
473  {
474  GNUNET_break_op (0);
475  return GNUNET_SYSERR;
476  }
477  val = json_integer_value (root);
478  if ((0 > val) || (val > UINT16_MAX))
479  {
480  GNUNET_break_op (0);
481  return GNUNET_SYSERR;
482  }
483  *up = (uint16_t) val;
484  return GNUNET_OK;
485 }

◆ GNUNET_JSON_spec_uint16()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint16 ( const char *  name,
uint16_t *  u16 
)

16-bit integer.

Parameters
namename of the JSON field
[out]u16where to store the integer found under name

Definition at line 326 of file json_helper.c.

491 {
492  struct GNUNET_JSON_Specification ret = {
493  .parser = &parse_u16,
494  .cleaner = NULL,
495  .cls = NULL,
496  .field = name,
497  .ptr = u16,
498  .ptr_size = sizeof(uint16_t),
499  .size_ptr = NULL
500  };
501 
502  return ret;
503 }
static enum GNUNET_GenericReturnValue parse_u16(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint16_t.
Definition: json_helper.c:465

◆ parse_u32()

static enum GNUNET_GenericReturnValue parse_u32 ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to a uint32_t.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 326 of file json_helper.c.

518 {
519  json_int_t val;
520  uint32_t *up = spec->ptr;
521 
522  if (! json_is_integer (root))
523  {
524  GNUNET_break_op (0);
525  return GNUNET_SYSERR;
526  }
527  val = json_integer_value (root);
528  if ((0 > val) || (val > UINT32_MAX))
529  {
530  GNUNET_break_op (0);
531  return GNUNET_SYSERR;
532  }
533  *up = (uint32_t) val;
534  return GNUNET_OK;
535 }

◆ GNUNET_JSON_spec_uint32()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint32 ( const char *  name,
uint32_t *  u32 
)

32-bit integer.

Parameters
namename of the JSON field
[out]u32where to store the integer found under name

Definition at line 326 of file json_helper.c.

541 {
542  struct GNUNET_JSON_Specification ret = {
543  .parser = &parse_u32,
544  .cleaner = NULL,
545  .cls = NULL,
546  .field = name,
547  .ptr = u32,
548  .ptr_size = sizeof(uint32_t),
549  .size_ptr = NULL
550  };
551 
552  return ret;
553 }
static enum GNUNET_GenericReturnValue parse_u32(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint32_t.
Definition: json_helper.c:515

◆ parse_u64()

static enum GNUNET_GenericReturnValue parse_u64 ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to a uint64_t.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 326 of file json_helper.c.

568 {
569  json_int_t val;
570  uint64_t *up = spec->ptr;
571 
572  if (! json_is_integer (root))
573  {
574  GNUNET_break_op (0);
575  return GNUNET_SYSERR;
576  }
577  val = json_integer_value (root);
578  *up = (uint64_t) val;
579  return GNUNET_OK;
580 }

◆ GNUNET_JSON_spec_uint64()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint64 ( const char *  name,
uint64_t *  u64 
)

64-bit integer.

Parameters
namename of the JSON field
[out]u64where to store the integer found under name

Definition at line 326 of file json_helper.c.

586 {
587  struct GNUNET_JSON_Specification ret = {
588  .parser = &parse_u64,
589  .cleaner = NULL,
590  .cls = NULL,
591  .field = name,
592  .ptr = u64,
593  .ptr_size = sizeof(uint64_t),
594  .size_ptr = NULL
595  };
596 
597  return ret;
598 }
static enum GNUNET_GenericReturnValue parse_u64(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint64_t.
Definition: json_helper.c:565

◆ parse_i64()

static enum GNUNET_GenericReturnValue parse_i64 ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to a int64_t.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 326 of file json_helper.c.

613 {
614  json_int_t val;
615  int64_t *up = spec->ptr;
616 
617  if (! json_is_integer (root))
618  {
619  GNUNET_break_op (0);
620  return GNUNET_SYSERR;
621  }
622  val = json_integer_value (root);
623  *up = (int64_t) val;
624  return GNUNET_OK;
625 }

◆ GNUNET_JSON_spec_int64()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_int64 ( const char *  name,
int64_t *  i64 
)

64-bit signed integer.

Parameters
namename of the JSON field
[out]i64where to store the integer found under name

Definition at line 326 of file json_helper.c.

631 {
632  struct GNUNET_JSON_Specification ret = {
633  .parser = &parse_i64,
634  .cleaner = NULL,
635  .cls = NULL,
636  .field = name,
637  .ptr = i64,
638  .ptr_size = sizeof(int64_t),
639  .size_ptr = NULL
640  };
641 
642  return ret;
643 }
static enum GNUNET_GenericReturnValue parse_i64(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a int64_t.
Definition: json_helper.c:610

◆ parse_timestamp()

static enum GNUNET_GenericReturnValue parse_timestamp ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to a timestamp.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 326 of file json_helper.c.

660 {
661  struct GNUNET_TIME_Timestamp *ts = spec->ptr;
662  json_t *json_t_s;
663  unsigned long long int tval;
664 
665  if (! json_is_object (root))
666  {
667  GNUNET_break_op (0);
668  return GNUNET_SYSERR;
669  }
670  json_t_s = json_object_get (root,
671  "t_s");
672  if (json_is_integer (json_t_s))
673  {
674  tval = json_integer_value (json_t_s);
675  /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */
677  = tval * GNUNET_TIME_UNIT_SECONDS.rel_value_us;
678  if (ts->abs_time.abs_value_us
679  / GNUNET_TIME_UNIT_SECONDS.rel_value_us
680  != tval)
681  {
682  /* Integer overflow */
683  GNUNET_break_op (0);
684  return GNUNET_SYSERR;
685  }
686  return GNUNET_OK;
687  }
688  if (json_is_string (json_t_s))
689  {
690  const char *val;
691 
692  val = json_string_value (json_t_s);
693  if ((0 == strcasecmp (val,
694  "never")))
695  {
697  return GNUNET_OK;
698  }
699  GNUNET_break_op (0);
700  return GNUNET_SYSERR;
701  }
702  GNUNET_break_op (0);
703  return GNUNET_SYSERR;
704 }
#define GNUNET_TIME_UNIT_SECONDS
One second.
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
uint64_t abs_value_us
The actual value.
Rounded time for timestamps used by GNUnet, in seconds.
struct GNUNET_TIME_Absolute abs_time
The actual value.

◆ GNUNET_JSON_spec_timestamp()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_timestamp ( const char *  name,
struct GNUNET_TIME_Timestamp t 
)

Timestamp.

Parameters
namename of the JSON field
[out]atwhere to store the absolute time found under name

Definition at line 326 of file json_helper.c.

710 {
711  struct GNUNET_JSON_Specification ret = {
712  .parser = &parse_timestamp,
713  .field = name,
714  .ptr = t,
715  .ptr_size = sizeof(struct GNUNET_TIME_Timestamp)
716  };
717 
718  return ret;
719 }
static struct GNUNET_SCHEDULER_Task * t
Main task.
static enum GNUNET_GenericReturnValue parse_timestamp(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a timestamp.
Definition: json_helper.c:657

◆ parse_timestamp_nbo()

static enum GNUNET_GenericReturnValue parse_timestamp_nbo ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to absolute time.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 326 of file json_helper.c.

734 {
735  struct GNUNET_TIME_TimestampNBO *ts = spec->ptr;
736  struct GNUNET_TIME_Timestamp a;
737  struct GNUNET_JSON_Specification ispec;
738 
739  ispec = *spec;
740  ispec.parser = &parse_timestamp;
741  ispec.ptr = &a;
742  if (GNUNET_OK !=
743  parse_timestamp (NULL,
744  root,
745  &ispec))
746  return GNUNET_SYSERR;
747  *ts = GNUNET_TIME_timestamp_hton (a);
748  return GNUNET_OK;
749 }
struct GNUNET_TIME_TimestampNBO GNUNET_TIME_timestamp_hton(struct GNUNET_TIME_Timestamp t)
Convert timestamp to network byte order.
Definition: time.c:90
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.
Time for timestamps used by GNUnet, in seconds and in network byte order.

◆ GNUNET_JSON_spec_timestamp_nbo()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_timestamp_nbo ( const char *  name,
struct GNUNET_TIME_TimestampNBO tn 
)

Timestamp in network byte order.

Parameters
namename of the JSON field
[out]tnwhere to store the absolute time found under name

Definition at line 326 of file json_helper.c.

755 {
756  struct GNUNET_JSON_Specification ret = {
757  .parser = &parse_timestamp_nbo,
758  .field = name,
759  .ptr = at,
760  .ptr_size = sizeof(struct GNUNET_TIME_TimestampNBO)
761  };
762 
763  return ret;
764 }
static enum GNUNET_GenericReturnValue parse_timestamp_nbo(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to absolute time.
Definition: json_helper.c:731

◆ parse_rel_time()

static enum GNUNET_GenericReturnValue parse_rel_time ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to relative time.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 326 of file json_helper.c.

779 {
780  struct GNUNET_TIME_Relative *rel = spec->ptr;
781  json_t *json_d_us;
782  unsigned long long int tval;
783 
784  if (! json_is_object (root))
785  {
786  GNUNET_break_op (0);
787  return GNUNET_SYSERR;
788  }
789  json_d_us = json_object_get (root,
790  "d_us");
791  if (json_is_integer (json_d_us))
792  {
793  tval = json_integer_value (json_d_us);
794  if (tval >= (1LLU << 53))
795  {
796  /* value is larger than allowed */
797  GNUNET_break_op (0);
798  return GNUNET_SYSERR;
799  }
800  rel->rel_value_us = tval;
801  return GNUNET_OK;
802  }
803  if (json_is_string (json_d_us))
804  {
805  const char *val;
806 
807  val = json_string_value (json_d_us);
808  if ((0 == strcasecmp (val,
809  "forever")))
810  {
812  return GNUNET_OK;
813  }
814  GNUNET_break_op (0);
815  return GNUNET_SYSERR;
816  }
817  GNUNET_break_op (0);
818  return GNUNET_SYSERR;
819 }
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.

◆ GNUNET_JSON_spec_relative_time()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_relative_time ( const char *  name,
struct GNUNET_TIME_Relative rt 
)

Relative time.

Parameters
namename of the JSON field
[out]rtwhere to store the relative time found under name

Definition at line 326 of file json_helper.c.

825 {
826  struct GNUNET_JSON_Specification ret = {
827  .parser = &parse_rel_time,
828  .field = name,
829  .ptr = rt,
830  .ptr_size = sizeof(struct GNUNET_TIME_Relative)
831  };
832 
833  return ret;
834 }
static enum GNUNET_GenericReturnValue parse_rel_time(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to relative time.
Definition: json_helper.c:776

◆ parse_rsa_public_key()

static enum GNUNET_GenericReturnValue parse_rsa_public_key ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to RSA public key.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 326 of file json_helper.c.

849 {
850  struct GNUNET_CRYPTO_RsaPublicKey **pk = spec->ptr;
851  const char *enc;
852  char *buf;
853  size_t len;
854  size_t buf_len;
855 
856  if (NULL == (enc = json_string_value (root)))
857  {
858  GNUNET_break_op (0);
859  return GNUNET_SYSERR;
860  }
861  len = strlen (enc);
862  buf_len = (len * 5) / 8;
863  buf = GNUNET_malloc (buf_len);
864  if (GNUNET_OK !=
866  len,
867  buf,
868  buf_len))
869  {
870  GNUNET_break_op (0);
871  GNUNET_free (buf);
872  return GNUNET_SYSERR;
873  }
874  if (NULL == (*pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
875  buf_len)))
876  {
877  GNUNET_break_op (0);
878  GNUNET_free (buf);
879  return GNUNET_SYSERR;
880  }
881  GNUNET_free (buf);
882  return GNUNET_OK;
883 }
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
static char buf[2048]
struct GNUNET_CRYPTO_RsaPublicKey * GNUNET_CRYPTO_rsa_public_key_decode(const char *buf, size_t len)
Decode the public key from the data-format back to the "normal", internal format.
Definition: crypto_rsa.c:422
#define GNUNET_malloc(size)
Wrapper around malloc.
The public information of an RSA key pair.
Definition: crypto_rsa.c:52

◆ clean_rsa_public_key()

static void clean_rsa_public_key ( void *  cls,
struct GNUNET_JSON_Specification spec 
)
static

Cleanup data left from parsing RSA public key.

Parameters
clsclosure, NULL
[out]specwhere to free the data

Definition at line 893 of file json_helper.c.

895 {
896  struct GNUNET_CRYPTO_RsaPublicKey **pk = spec->ptr;
897 
898  if (NULL != *pk)
899  {
901  *pk = NULL;
902  }
903 }
void GNUNET_CRYPTO_rsa_public_key_free(struct GNUNET_CRYPTO_RsaPublicKey *key)
Free memory occupied by the public key.
Definition: crypto_rsa.c:267

References GNUNET_CRYPTO_rsa_public_key_free(), pk, and GNUNET_JSON_Specification::ptr.

Here is the call graph for this function:

◆ GNUNET_JSON_spec_rsa_public_key()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_public_key ( const char *  name,
struct GNUNET_CRYPTO_RsaPublicKey **  pk 
)

Specification for parsing an RSA public key.

Parameters
namename of the JSON field
pkwhere to store the RSA key found under name

Definition at line 893 of file json_helper.c.

909 {
910  struct GNUNET_JSON_Specification ret = {
911  .parser = &parse_rsa_public_key,
912  .cleaner = &clean_rsa_public_key,
913  .field = name,
914  .ptr = pk
915  };
916 
917  *pk = NULL;
918  return ret;
919 }
static void clean_rsa_public_key(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_helper.c:893
static enum GNUNET_GenericReturnValue parse_rsa_public_key(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to RSA public key.
Definition: json_helper.c:846

◆ parse_rsa_signature()

static enum GNUNET_GenericReturnValue parse_rsa_signature ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to RSA signature.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 893 of file json_helper.c.

934 {
935  struct GNUNET_CRYPTO_RsaSignature **sig = spec->ptr;
936  size_t size;
937  const char *str;
938  int res;
939  void *buf;
940 
941  str = json_string_value (root);
942  if (NULL == str)
943  {
944  GNUNET_break_op (0);
945  return GNUNET_SYSERR;
946  }
947  size = (strlen (str) * 5) / 8;
948  buf = GNUNET_malloc (size);
950  strlen (str),
951  buf,
952  size);
953  if (GNUNET_OK != res)
954  {
955  GNUNET_free (buf);
956  GNUNET_break_op (0);
957  return GNUNET_SYSERR;
958  }
959  if (NULL == (*sig = GNUNET_CRYPTO_rsa_signature_decode (buf,
960  size)))
961  {
962  GNUNET_break_op (0);
963  GNUNET_free (buf);
964  return GNUNET_SYSERR;
965  }
966  GNUNET_free (buf);
967  return GNUNET_OK;
968 }
static int res
struct GNUNET_CRYPTO_RsaSignature * GNUNET_CRYPTO_rsa_signature_decode(const void *buf, size_t buf_size)
Decode the signature from the data-format back to the "normal", internal format.
Definition: crypto_rsa.c:1038
an RSA signature
Definition: crypto_rsa.c:64

◆ clean_rsa_signature()

static void clean_rsa_signature ( void *  cls,
struct GNUNET_JSON_Specification spec 
)
static

Cleanup data left from parsing RSA signature.

Parameters
clsclosure, NULL
[out]specwhere to free the data

Definition at line 978 of file json_helper.c.

980 {
981  struct GNUNET_CRYPTO_RsaSignature **sig = spec->ptr;
982 
983  if (NULL != *sig)
984  {
986  *sig = NULL;
987  }
988 }
void GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig)
Free memory occupied by signature.
Definition: crypto_rsa.c:990

References GNUNET_CRYPTO_rsa_signature_free(), and GNUNET_JSON_Specification::ptr.

Here is the call graph for this function:

◆ GNUNET_JSON_spec_rsa_signature()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_signature ( const char *  name,
struct GNUNET_CRYPTO_RsaSignature **  sig 
)

Specification for parsing an RSA signature.

Parameters
namename of the JSON field
sigwhere to store the RSA signature found under name

Definition at line 978 of file json_helper.c.

994 {
995  struct GNUNET_JSON_Specification ret = {
996  .parser = &parse_rsa_signature,
997  .cleaner = &clean_rsa_signature,
998  .cls = NULL,
999  .field = name,
1000  .ptr = sig,
1001  .ptr_size = 0,
1002  .size_ptr = NULL
1003  };
1004 
1005  *sig = NULL;
1006  return ret;
1007 }
static enum GNUNET_GenericReturnValue parse_rsa_signature(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to RSA signature.
Definition: json_helper.c:931
static void clean_rsa_signature(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA signature.
Definition: json_helper.c:978

◆ parse_boolean()

static enum GNUNET_GenericReturnValue parse_boolean ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Parse given JSON object to an int as a boolean.

Parameters
clsclosure, NULL
rootthe json object representing data
[out]specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 978 of file json_helper.c.

1022 {
1023  int *bp = spec->ptr;
1024 
1025  if (! json_is_boolean (root))
1026  {
1027  GNUNET_break_op (0);
1028  return GNUNET_SYSERR;
1029  }
1030  *bp = json_boolean_value (root) ? GNUNET_YES : GNUNET_NO;
1031  return GNUNET_OK;
1032 }
@ GNUNET_YES
@ GNUNET_NO
Definition: gnunet_common.h:98

◆ GNUNET_JSON_spec_boolean()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_boolean ( const char *  name,
int *  boolean 
)

Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO).

Parameters
namename of the JSON field
[out]booleanwhere to store the boolean found under name

Definition at line 978 of file json_helper.c.

1038 {
1039  struct GNUNET_JSON_Specification ret = {
1040  .parser = &parse_boolean,
1041  .cleaner = NULL,
1042  .cls = NULL,
1043  .field = name,
1044  .ptr = boolean,
1045  .ptr_size = sizeof(int),
1046  .size_ptr = NULL
1047  };
1048 
1049  return ret;
1050 }
static enum GNUNET_GenericReturnValue parse_boolean(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to an int as a boolean.
Definition: json_helper.c:1019