GNUnet  0.11.x
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 int 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 int 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 int 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 int 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 int 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 int 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 int 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 int 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 int 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);
69  return GNUNET_SYSERR;
70  }
71  if (GNUNET_OK !=
73  len,
74  spec->ptr,
75  spec->ptr_size))
76  {
77  GNUNET_break_op (0);
78  return GNUNET_SYSERR;
79  }
80  return GNUNET_OK;
81 }
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...
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
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:762
void * ptr
Pointer, details specific to the parser.
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.

88 {
90  .parser = &parse_fixed_data,
91  .cleaner = NULL,
92  .cls = NULL,
93  .field = name,
94  .ptr = obj,
95  .ptr_size = size,
96  .size_ptr = NULL
97  };
98 
99  return ret;
100 }
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_variable_data()

static int 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 112 of file json_helper.c.

115 {
116  const char *str;
117  size_t size;
118  void *data;
119 
120  str = json_string_value (root);
121  if (NULL == str)
122  {
123  GNUNET_break_op (0);
124  return GNUNET_SYSERR;
125  }
126  if (GNUNET_OK !=
128  strlen (str),
129  &data,
130  &size))
131  {
132  GNUNET_break_op (0);
133  return GNUNET_SYSERR;
134  }
135  *(void **) spec->ptr = data;
136  *spec->size_ptr = size;
137  return GNUNET_OK;
138 }
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:828
size_t * size_ptr
Where should we store the final size of ptr.

References data, GNUNET_break_op, GNUNET_OK, GNUNET_STRINGS_string_to_data_alloc(), GNUNET_SYSERR, GNUNET_JSON_Specification::ptr, size, and GNUNET_JSON_Specification::size_ptr.

Here is the call graph for this function:

◆ 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 148 of file json_helper.c.

150 {
151  (void) cls;
152  if (0 != *spec->size_ptr)
153  {
154  GNUNET_free (*(void **) spec->ptr);
155  *(void **) spec->ptr = NULL;
156  *spec->size_ptr = 0;
157  }
158 }
#define GNUNET_free(ptr)
Wrapper around free.

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 148 of file json_helper.c.

165 {
166  struct GNUNET_JSON_Specification ret = {
167  .parser = &parse_variable_data,
168  .cleaner = &clean_variable_data,
169  .cls = NULL,
170  .field = name,
171  .ptr = obj,
172  .ptr_size = 0,
173  .size_ptr = size
174  };
175 
176  *obj = NULL;
177  *size = 0;
178  return ret;
179 }
static int 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:112
static void clean_variable_data(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing variable size data.
Definition: json_helper.c:148

◆ parse_string()

static int 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 191 of file json_helper.c.

194 {
195  const char *str;
196 
197  (void) cls;
198  str = json_string_value (root);
199  if (NULL == str)
200  {
201  GNUNET_break_op (0);
202  return GNUNET_SYSERR;
203  }
204  *(const char **) spec->ptr = str;
205  return GNUNET_OK;
206 }

◆ 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 191 of file json_helper.c.

212 {
213  struct GNUNET_JSON_Specification ret = {
214  .parser = &parse_string,
215  .cleaner = NULL,
216  .cls = NULL,
217  .field = name,
218  .ptr = strptr,
219  .ptr_size = 0,
220  .size_ptr = NULL
221  };
222 
223  *strptr = NULL;
224  return ret;
225 }
static int parse_string(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to string.
Definition: json_helper.c:191

References GNUNET_JSON_Specification::cls, GNUNET_break_op, GNUNET_OK, GNUNET_SYSERR, and GNUNET_JSON_Specification::ptr.

◆ parse_object()

static int 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 237 of file json_helper.c.

240 {
241  if (! (json_is_object (root) || json_is_array (root)))
242  {
243  GNUNET_break_op (0);
244  return GNUNET_SYSERR;
245  }
246  json_incref (root);
247  *(json_t **) spec->ptr = root;
248  return GNUNET_OK;
249 }

References GNUNET_break_op, GNUNET_OK, GNUNET_SYSERR, and GNUNET_JSON_Specification::ptr.

◆ 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 259 of file json_helper.c.

261 {
262  json_t **ptr = (json_t **) spec->ptr;
263 
264  if (NULL != *ptr)
265  {
266  json_decref (*ptr);
267  *ptr = NULL;
268  }
269 }

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 259 of file json_helper.c.

275 {
276  struct GNUNET_JSON_Specification ret = {
277  .parser = &parse_object,
278  .cleaner = &clean_object,
279  .cls = NULL,
280  .field = name,
281  .ptr = jsonp,
282  .ptr_size = 0,
283  .size_ptr = NULL
284  };
285 
286  *jsonp = NULL;
287  return ret;
288 }
static void clean_object(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing JSON object.
Definition: json_helper.c:259
static int parse_object(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a JSON object.
Definition: json_helper.c:237

◆ parse_bool()

static int 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 300 of file json_helper.c.

303 {
304  bool *b = spec->ptr;
305 
306  if (json_true () == root)
307  {
308  *b = true;
309  return GNUNET_OK;
310  }
311  if (json_false () == root)
312  {
313  *b = false;
314  return GNUNET_OK;
315  }
316  GNUNET_break_op (0);
317  return GNUNET_SYSERR;
318 }

◆ 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 300 of file json_helper.c.

324 {
325  struct GNUNET_JSON_Specification ret = {
326  .parser = &parse_bool,
327  .cleaner = NULL,
328  .cls = NULL,
329  .field = name,
330  .ptr = b,
331  .ptr_size = sizeof(bool),
332  .size_ptr = NULL
333  };
334 
335  return ret;
336 }
static int parse_bool(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a bool.
Definition: json_helper.c:300

References GNUNET_break_op, GNUNET_OK, GNUNET_SYSERR, and GNUNET_JSON_Specification::ptr.

◆ parse_u8()

static int 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 348 of file json_helper.c.

351 {
352  json_int_t val;
353  uint8_t *up = spec->ptr;
354 
355  if (! json_is_integer (root))
356  {
357  GNUNET_break_op (0);
358  return GNUNET_SYSERR;
359  }
360  val = json_integer_value (root);
361  if ((0 > val) || (val > UINT8_MAX))
362  {
363  GNUNET_break_op (0);
364  return GNUNET_SYSERR;
365  }
366  *up = (uint8_t) val;
367  return GNUNET_OK;
368 }

◆ 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 348 of file json_helper.c.

374 {
375  struct GNUNET_JSON_Specification ret = {
376  .parser = &parse_u8,
377  .cleaner = NULL,
378  .cls = NULL,
379  .field = name,
380  .ptr = u8,
381  .ptr_size = sizeof(uint8_t),
382  .size_ptr = NULL
383  };
384 
385  return ret;
386 }
static int parse_u8(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint8_t.
Definition: json_helper.c:348

References GNUNET_break_op, GNUNET_OK, GNUNET_SYSERR, and GNUNET_JSON_Specification::ptr.

◆ parse_u16()

static int 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 398 of file json_helper.c.

401 {
402  json_int_t val;
403  uint16_t *up = spec->ptr;
404 
405  if (! json_is_integer (root))
406  {
407  GNUNET_break_op (0);
408  return GNUNET_SYSERR;
409  }
410  val = json_integer_value (root);
411  if ((0 > val) || (val > UINT16_MAX))
412  {
413  GNUNET_break_op (0);
414  return GNUNET_SYSERR;
415  }
416  *up = (uint16_t) val;
417  return GNUNET_OK;
418 }

◆ 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 398 of file json_helper.c.

424 {
425  struct GNUNET_JSON_Specification ret = {
426  .parser = &parse_u16,
427  .cleaner = NULL,
428  .cls = NULL,
429  .field = name,
430  .ptr = u16,
431  .ptr_size = sizeof(uint16_t),
432  .size_ptr = NULL
433  };
434 
435  return ret;
436 }
static int parse_u16(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint16_t.
Definition: json_helper.c:398

References GNUNET_break_op, GNUNET_OK, GNUNET_SYSERR, and GNUNET_JSON_Specification::ptr.

◆ parse_u32()

static int 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 448 of file json_helper.c.

451 {
452  json_int_t val;
453  uint32_t *up = spec->ptr;
454 
455  if (! json_is_integer (root))
456  {
457  GNUNET_break_op (0);
458  return GNUNET_SYSERR;
459  }
460  val = json_integer_value (root);
461  if ((0 > val) || (val > UINT32_MAX))
462  {
463  GNUNET_break_op (0);
464  return GNUNET_SYSERR;
465  }
466  *up = (uint32_t) val;
467  return GNUNET_OK;
468 }

◆ 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 448 of file json_helper.c.

474 {
475  struct GNUNET_JSON_Specification ret = {
476  .parser = &parse_u32,
477  .cleaner = NULL,
478  .cls = NULL,
479  .field = name,
480  .ptr = u32,
481  .ptr_size = sizeof(uint32_t),
482  .size_ptr = NULL
483  };
484 
485  return ret;
486 }
static int parse_u32(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint32_t.
Definition: json_helper.c:448

References GNUNET_break_op, GNUNET_OK, GNUNET_SYSERR, and GNUNET_JSON_Specification::ptr.

◆ parse_u64()

static int 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 498 of file json_helper.c.

501 {
502  json_int_t val;
503  uint64_t *up = spec->ptr;
504 
505  if (! json_is_integer (root))
506  {
507  GNUNET_break_op (0);
508  return GNUNET_SYSERR;
509  }
510  val = json_integer_value (root);
511  *up = (uint64_t) val;
512  return GNUNET_OK;
513 }

◆ 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 498 of file json_helper.c.

519 {
520  struct GNUNET_JSON_Specification ret = {
521  .parser = &parse_u64,
522  .cleaner = NULL,
523  .cls = NULL,
524  .field = name,
525  .ptr = u64,
526  .ptr_size = sizeof(uint64_t),
527  .size_ptr = NULL
528  };
529 
530  return ret;
531 }
static int parse_u64(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint64_t.
Definition: json_helper.c:498

References GNUNET_break_op, GNUNET_OK, GNUNET_SYSERR, and GNUNET_JSON_Specification::ptr.

◆ parse_i64()

static int 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 543 of file json_helper.c.

546 {
547  json_int_t val;
548  int64_t *up = spec->ptr;
549 
550  if (! json_is_integer (root))
551  {
552  GNUNET_break_op (0);
553  return GNUNET_SYSERR;
554  }
555  val = json_integer_value (root);
556  *up = (int64_t) val;
557  return GNUNET_OK;
558 }

◆ 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 543 of file json_helper.c.

564 {
565  struct GNUNET_JSON_Specification ret = {
566  .parser = &parse_i64,
567  .cleaner = NULL,
568  .cls = NULL,
569  .field = name,
570  .ptr = i64,
571  .ptr_size = sizeof(int64_t),
572  .size_ptr = NULL
573  };
574 
575  return ret;
576 }
static int parse_i64(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a int64_t.
Definition: json_helper.c:543

References GNUNET_break_op, GNUNET_OK, GNUNET_SYSERR, and GNUNET_JSON_Specification::ptr.

◆ 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 543 of file json_helper.c.

593 {
594  struct GNUNET_TIME_Timestamp *ts = spec->ptr;
595  json_t *json_t_s;
596  json_t *json_t_ms;
597  unsigned long long int tval;
598 
599  if (! json_is_object (root))
600  {
601  GNUNET_break_op (0);
602  return GNUNET_SYSERR;
603  }
604  json_t_s = json_object_get (root,
605  "t_s");
606  if (json_is_integer (json_t_s))
607  {
608  tval = json_integer_value (json_t_s);
609  /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */
611  = tval * GNUNET_TIME_UNIT_SECONDS.rel_value_us;
612  if (ts->abs_time.abs_value_us
613  / GNUNET_TIME_UNIT_SECONDS.rel_value_us
614  != tval)
615  {
616  /* Integer overflow */
617  GNUNET_break_op (0);
618  return GNUNET_SYSERR;
619  }
620  return GNUNET_OK;
621  }
622  if (json_is_string (json_t_s))
623  {
624  const char *val;
625 
626  val = json_string_value (json_t_s);
627  if ((0 == strcasecmp (val,
628  "never")))
629  {
631  return GNUNET_OK;
632  }
633  GNUNET_break_op (0);
634  return GNUNET_SYSERR;
635  }
636  json_t_ms = json_object_get (root,
637  "t_ms");
638  if (json_is_integer (json_t_ms))
639  {
640  tval = json_integer_value (json_t_ms);
641  GNUNET_break_op (0 == tval % 1000);
642  tval -= tval % 1000;
643  /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */
645  = tval * GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us;
646  if (ts->abs_time.abs_value_us
647  / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us
648  != tval)
649  {
650  /* Integer overflow */
651  GNUNET_break_op (0);
652  return GNUNET_SYSERR;
653  }
654  return GNUNET_OK;
655  }
656  if (json_is_string (json_t_ms))
657  {
658  const char *val;
659 
660  val = json_string_value (json_t_ms);
661  if ((0 == strcasecmp (val,
662  "never")))
663  {
665  return GNUNET_OK;
666  }
667  GNUNET_break_op (0);
668  return GNUNET_SYSERR;
669  }
670  GNUNET_break_op (0);
671  return GNUNET_SYSERR;
672 }
#define GNUNET_TIME_UNIT_SECONDS
One second.
#define GNUNET_TIME_UNIT_MILLISECONDS
One millisecond.
#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 543 of file json_helper.c.

678 {
679  struct GNUNET_JSON_Specification ret = {
680  .parser = &parse_timestamp,
681  .field = name,
682  .ptr = t,
683  .ptr_size = sizeof(struct GNUNET_TIME_Timestamp)
684  };
685 
686  return ret;
687 }
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:590

◆ 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 543 of file json_helper.c.

702 {
703  struct GNUNET_TIME_TimestampNBO *ts = spec->ptr;
704  struct GNUNET_TIME_Timestamp a;
705  struct GNUNET_JSON_Specification ispec;
706 
707  ispec = *spec;
708  ispec.parser = &parse_timestamp;
709  ispec.ptr = &a;
710  if (GNUNET_OK !=
711  parse_timestamp (NULL,
712  root,
713  &ispec))
714  return GNUNET_SYSERR;
715  *ts = GNUNET_TIME_timestamp_hton (a);
716  return GNUNET_OK;
717 }
struct GNUNET_TIME_TimestampNBO GNUNET_TIME_timestamp_hton(struct GNUNET_TIME_Timestamp t)
Convert timestamp to network byte order.
Definition: time.c:74
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 543 of file json_helper.c.

723 {
724  struct GNUNET_JSON_Specification ret = {
725  .parser = &parse_timestamp_nbo,
726  .field = name,
727  .ptr = at,
728  .ptr_size = sizeof(struct GNUNET_TIME_TimestampNBO)
729  };
730 
731  return ret;
732 }
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:699

◆ 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 543 of file json_helper.c.

747 {
748  struct GNUNET_TIME_Relative *rel = spec->ptr;
749  json_t *json_d_us;
750  json_t *json_d_ms;
751  unsigned long long int tval;
752 
753  if (! json_is_object (root))
754  {
755  GNUNET_break_op (0);
756  return GNUNET_SYSERR;
757  }
758  json_d_us = json_object_get (root,
759  "d_us");
760  if (json_is_integer (json_d_us))
761  {
762  tval = json_integer_value (json_d_us);
763  if (tval >= (1LLU << 53))
764  {
765  /* value is larger than allowed */
766  GNUNET_break_op (0);
767  return GNUNET_SYSERR;
768  }
769  rel->rel_value_us = tval;
770  return GNUNET_OK;
771  }
772  if (json_is_string (json_d_us))
773  {
774  const char *val;
775 
776  val = json_string_value (json_d_us);
777  if ((0 == strcasecmp (val,
778  "forever")))
779  {
781  return GNUNET_OK;
782  }
783  GNUNET_break_op (0);
784  return GNUNET_SYSERR;
785  }
786 
787  json_d_ms = json_object_get (root,
788  "d_ms");
789  if (json_is_integer (json_d_ms))
790  {
791  tval = json_integer_value (json_d_ms);
794  tval);
795  return GNUNET_OK;
796  }
797  if (json_is_string (json_d_ms))
798  {
799  const char *val;
800 
801  val = json_string_value (json_d_ms);
802  if ((0 == strcasecmp (val,
803  "forever")))
804  {
806  return GNUNET_OK;
807  }
808  GNUNET_break_op (0);
809  return GNUNET_SYSERR;
810  }
811  GNUNET_break_op (0);
812  return GNUNET_SYSERR;
813 }
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:453
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 543 of file json_helper.c.

819 {
820  struct GNUNET_JSON_Specification ret = {
821  .parser = &parse_rel_time,
822  .field = name,
823  .ptr = rt,
824  .ptr_size = sizeof(struct GNUNET_TIME_Relative)
825  };
826 
827  return ret;
828 }
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:744

◆ 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 543 of file json_helper.c.

843 {
844  struct GNUNET_CRYPTO_RsaPublicKey **pk = spec->ptr;
845  const char *enc;
846  char *buf;
847  size_t len;
848  size_t buf_len;
849 
850  if (NULL == (enc = json_string_value (root)))
851  {
852  GNUNET_break_op (0);
853  return GNUNET_SYSERR;
854  }
855  len = strlen (enc);
856  buf_len = (len * 5) / 8;
857  buf = GNUNET_malloc (buf_len);
858  if (GNUNET_OK !=
860  len,
861  buf,
862  buf_len))
863  {
864  GNUNET_break_op (0);
865  GNUNET_free (buf);
866  return GNUNET_SYSERR;
867  }
868  if (NULL == (*pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
869  buf_len)))
870  {
871  GNUNET_break_op (0);
872  GNUNET_free (buf);
873  return GNUNET_SYSERR;
874  }
875  GNUNET_free (buf);
876  return GNUNET_OK;
877 }
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 887 of file json_helper.c.

889 {
890  struct GNUNET_CRYPTO_RsaPublicKey **pk = spec->ptr;
891 
892  if (NULL != *pk)
893  {
895  *pk = NULL;
896  }
897 }
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 887 of file json_helper.c.

903 {
904  struct GNUNET_JSON_Specification ret = {
905  .parser = &parse_rsa_public_key,
906  .cleaner = &clean_rsa_public_key,
907  .field = name,
908  .ptr = pk
909  };
910 
911  *pk = NULL;
912  return ret;
913 }
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:887
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:840

◆ 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 887 of file json_helper.c.

928 {
929  struct GNUNET_CRYPTO_RsaSignature **sig = spec->ptr;
930  size_t size;
931  const char *str;
932  int res;
933  void *buf;
934 
935  str = json_string_value (root);
936  if (NULL == str)
937  {
938  GNUNET_break_op (0);
939  return GNUNET_SYSERR;
940  }
941  size = (strlen (str) * 5) / 8;
942  buf = GNUNET_malloc (size);
944  strlen (str),
945  buf,
946  size);
947  if (GNUNET_OK != res)
948  {
949  GNUNET_free (buf);
950  GNUNET_break_op (0);
951  return GNUNET_SYSERR;
952  }
953  if (NULL == (*sig = GNUNET_CRYPTO_rsa_signature_decode (buf,
954  size)))
955  {
956  GNUNET_break_op (0);
957  GNUNET_free (buf);
958  return GNUNET_SYSERR;
959  }
960  GNUNET_free (buf);
961  return GNUNET_OK;
962 }
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:1032
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 972 of file json_helper.c.

974 {
975  struct GNUNET_CRYPTO_RsaSignature **sig = spec->ptr;
976 
977  if (NULL != *sig)
978  {
980  *sig = NULL;
981  }
982 }
void GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig)
Free memory occupied by signature.
Definition: crypto_rsa.c:984

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 972 of file json_helper.c.

988 {
989  struct GNUNET_JSON_Specification ret = {
990  .parser = &parse_rsa_signature,
991  .cleaner = &clean_rsa_signature,
992  .cls = NULL,
993  .field = name,
994  .ptr = sig,
995  .ptr_size = 0,
996  .size_ptr = NULL
997  };
998 
999  *sig = NULL;
1000  return ret;
1001 }
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:925
static void clean_rsa_signature(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA signature.
Definition: json_helper.c:972

◆ 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 972 of file json_helper.c.

1016 {
1017  int *bp = spec->ptr;
1018 
1019  if (! json_is_boolean (root))
1020  {
1021  GNUNET_break_op (0);
1022  return GNUNET_SYSERR;
1023  }
1024  *bp = json_boolean_value (root) ? GNUNET_YES : GNUNET_NO;
1025  return GNUNET_OK;
1026 }
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94

◆ 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 972 of file json_helper.c.

1032 {
1033  struct GNUNET_JSON_Specification ret = {
1034  .parser = &parse_boolean,
1035  .cleaner = NULL,
1036  .cls = NULL,
1037  .field = name,
1038  .ptr = boolean,
1039  .ptr_size = sizeof(int),
1040  .size_ptr = NULL
1041  };
1042 
1043  return ret;
1044 }
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:1013