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 int 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 int parse_abs_time (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_absolute_time (const char *name, struct GNUNET_TIME_Absolute *at)
 Absolute time. More...
 
static int parse_abs_time_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_absolute_time_nbo (const char *name, struct GNUNET_TIME_AbsoluteNBO *at)
 Absolute time in network byte order. More...
 
static int 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 int 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 int 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 int 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 int 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 53 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:775
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 53 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 int 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

References enc, GNUNET_break_op, GNUNET_OK, GNUNET_STRINGS_string_to_data(), GNUNET_SYSERR, len, GNUNET_JSON_Specification::ptr, and GNUNET_JSON_Specification::ptr_size.

Here is the call graph for this function:

◆ 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:841
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 testconfigure::b, 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_abs_time()

static int parse_abs_time ( 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 590 of file json_helper.c.

593 {
594  struct GNUNET_TIME_Absolute *abs = spec->ptr;
595  json_t *json_t_ms;
596  unsigned long long int tval;
597 
598  if (! json_is_object (root))
599  {
600  GNUNET_break_op (0);
601  return GNUNET_SYSERR;
602  }
603  json_t_ms = json_object_get (root, "t_ms");
604  if (json_is_integer (json_t_ms))
605  {
606  tval = json_integer_value (json_t_ms);
607  /* Time is in milliseconds in JSON, but in microseconds in GNUNET_TIME_Absolute */
608  abs->abs_value_us = tval * GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us;
609  if ((abs->abs_value_us)
610  / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us
611  != tval)
612  {
613  /* Integer overflow */
614  GNUNET_break_op (0);
615  return GNUNET_SYSERR;
616  }
617  return GNUNET_OK;
618  }
619  if (json_is_string (json_t_ms))
620  {
621  const char *val;
622 
623  val = json_string_value (json_t_ms);
624  if ((0 == strcasecmp (val, "never")))
625  {
627  return GNUNET_OK;
628  }
629  GNUNET_break_op (0);
630  return GNUNET_SYSERR;
631  }
632  GNUNET_break_op (0);
633  return GNUNET_SYSERR;
634 }
#define GNUNET_TIME_UNIT_MILLISECONDS
One millisecond.
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.

Referenced by GNUNET_JSON_spec_absolute_time_nbo().

Here is the caller graph for this function:

◆ GNUNET_JSON_spec_absolute_time()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_absolute_time ( const char *  name,
struct GNUNET_TIME_Absolute at 
)

Absolute time.

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

Definition at line 590 of file json_helper.c.

640 {
641  struct GNUNET_JSON_Specification ret = {
642  .parser = &parse_abs_time,
643  .cleaner = NULL,
644  .cls = NULL,
645  .field = name,
646  .ptr = at,
647  .ptr_size = sizeof(struct GNUNET_TIME_Absolute),
648  .size_ptr = NULL
649  };
650 
651  return ret;
652 }
static int parse_abs_time(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to absolute time.
Definition: json_helper.c:590

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_break_op, GNUNET_OK, GNUNET_SYSERR, GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_MILLISECONDS, and GNUNET_JSON_Specification::ptr.

◆ parse_abs_time_nbo()

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

667 {
668  struct GNUNET_TIME_AbsoluteNBO *abs = spec->ptr;
669  struct GNUNET_TIME_Absolute a;
670  struct GNUNET_JSON_Specification ispec;
671 
672  ispec = *spec;
673  ispec.parser = &parse_abs_time;
674  ispec.ptr = &a;
675  if (GNUNET_OK !=
676  parse_abs_time (NULL,
677  root,
678  &ispec))
679  return GNUNET_SYSERR;
680  *abs = GNUNET_TIME_absolute_hton (a);
681  return GNUNET_OK;
682 }
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:464
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.
Time for absolute time used by GNUnet, in microseconds and in network byte order.

◆ GNUNET_JSON_spec_absolute_time_nbo()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_absolute_time_nbo ( const char *  name,
struct GNUNET_TIME_AbsoluteNBO at 
)

Absolute time in network byte order.

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

Definition at line 664 of file json_helper.c.

688 {
689  struct GNUNET_JSON_Specification ret = {
690  .parser = &parse_abs_time_nbo,
691  .cleaner = NULL,
692  .cls = NULL,
693  .field = name,
694  .ptr = at,
695  .ptr_size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
696  .size_ptr = NULL
697  };
698 
699  return ret;
700 }
static int parse_abs_time_nbo(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to absolute time.
Definition: json_helper.c:664

References GNUNET_OK, GNUNET_SYSERR, GNUNET_TIME_absolute_hton(), parse_abs_time(), GNUNET_JSON_Specification::parser, and GNUNET_JSON_Specification::ptr.

Here is the call graph for this function:

◆ parse_rel_time()

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

715 {
716  struct GNUNET_TIME_Relative *rel = spec->ptr;
717  json_t *json_d_ms;
718  unsigned long long int tval;
719 
720  if (! json_is_object (root))
721  {
722  GNUNET_break_op (0);
723  return GNUNET_SYSERR;
724  }
725  json_d_ms = json_object_get (root, "d_ms");
726  if (json_is_integer (json_d_ms))
727  {
728  tval = json_integer_value (json_d_ms);
729  /* Time is in milliseconds in JSON, but in microseconds in GNUNET_TIME_Absolute */
730  rel->rel_value_us = tval * 1000LL;
731  if ((rel->rel_value_us) / 1000LL != tval)
732  {
733  /* Integer overflow */
734  GNUNET_break_op (0);
735  return GNUNET_SYSERR;
736  }
737  return GNUNET_OK;
738  }
739  if (json_is_string (json_d_ms))
740  {
741  const char *val;
742  val = json_string_value (json_d_ms);
743  if ((0 == strcasecmp (val, "forever")))
744  {
746  return GNUNET_OK;
747  }
748  GNUNET_break_op (0);
749  return GNUNET_SYSERR;
750  }
751  GNUNET_break_op (0);
752  return GNUNET_SYSERR;
753 }
#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 712 of file json_helper.c.

759 {
760  struct GNUNET_JSON_Specification ret = {
761  .parser = &parse_rel_time,
762  .cleaner = NULL,
763  .cls = NULL,
764  .field = name,
765  .ptr = rt,
766  .ptr_size = sizeof(struct GNUNET_TIME_Relative),
767  .size_ptr = NULL
768  };
769 
770  return ret;
771 }
static int parse_rel_time(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to relative time.
Definition: json_helper.c:712

References GNUNET_break_op, GNUNET_OK, GNUNET_SYSERR, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_JSON_Specification::ptr, and GNUNET_TIME_Relative::rel_value_us.

◆ parse_rsa_public_key()

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

786 {
787  struct GNUNET_CRYPTO_RsaPublicKey **pk = spec->ptr;
788  const char *enc;
789  char *buf;
790  size_t len;
791  size_t buf_len;
792 
793  if (NULL == (enc = json_string_value (root)))
794  {
795  GNUNET_break_op (0);
796  return GNUNET_SYSERR;
797  }
798  len = strlen (enc);
799  buf_len = (len * 5) / 8;
800  buf = GNUNET_malloc (buf_len);
801  if (GNUNET_OK !=
803  len,
804  buf,
805  buf_len))
806  {
807  GNUNET_break_op (0);
808  GNUNET_free (buf);
809  return GNUNET_SYSERR;
810  }
811  if (NULL == (*pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
812  buf_len)))
813  {
814  GNUNET_break_op (0);
815  GNUNET_free (buf);
816  return GNUNET_SYSERR;
817  }
818  GNUNET_free (buf);
819  return GNUNET_OK;
820 }
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:383
#define GNUNET_malloc(size)
Wrapper around malloc.
The public information of an RSA key pair.
Definition: crypto_rsa.c:52

References buf, enc, GNUNET_break_op, GNUNET_CRYPTO_rsa_public_key_decode(), GNUNET_free, GNUNET_malloc, GNUNET_OK, GNUNET_STRINGS_string_to_data(), GNUNET_SYSERR, len, pk, and GNUNET_JSON_Specification::ptr.

Here is the call graph for this function:

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

832 {
833  struct GNUNET_CRYPTO_RsaPublicKey **pk = spec->ptr;
834 
835  if (NULL != *pk)
836  {
838  *pk = NULL;
839  }
840 }
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 830 of file json_helper.c.

846 {
847  struct GNUNET_JSON_Specification ret = {
848  .parser = &parse_rsa_public_key,
849  .cleaner = &clean_rsa_public_key,
850  .cls = NULL,
851  .field = name,
852  .ptr = pk,
853  .ptr_size = 0,
854  .size_ptr = NULL
855  };
856 
857  *pk = NULL;
858  return ret;
859 }
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:830
static int 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:783

◆ parse_rsa_signature()

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

874 {
875  struct GNUNET_CRYPTO_RsaSignature **sig = spec->ptr;
876  size_t size;
877  const char *str;
878  int res;
879  void *buf;
880 
881  str = json_string_value (root);
882  if (NULL == str)
883  {
884  GNUNET_break_op (0);
885  return GNUNET_SYSERR;
886  }
887  size = (strlen (str) * 5) / 8;
888  buf = GNUNET_malloc (size);
890  strlen (str),
891  buf,
892  size);
893  if (GNUNET_OK != res)
894  {
895  GNUNET_free (buf);
896  GNUNET_break_op (0);
897  return GNUNET_SYSERR;
898  }
899  if (NULL == (*sig = GNUNET_CRYPTO_rsa_signature_decode (buf,
900  size)))
901  {
902  GNUNET_break_op (0);
903  GNUNET_free (buf);
904  return GNUNET_SYSERR;
905  }
906  GNUNET_free (buf);
907  return GNUNET_OK;
908 }
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:993
an RSA signature
Definition: crypto_rsa.c:64

References buf, GNUNET_break_op, GNUNET_CRYPTO_rsa_signature_decode(), GNUNET_free, GNUNET_malloc, GNUNET_OK, GNUNET_STRINGS_string_to_data(), GNUNET_SYSERR, GNUNET_JSON_Specification::ptr, res, and size.

Here is the call graph for this function:

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

920 {
921  struct GNUNET_CRYPTO_RsaSignature **sig = spec->ptr;
922 
923  if (NULL != *sig)
924  {
926  *sig = NULL;
927  }
928 }
void GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig)
Free memory occupied by signature.
Definition: crypto_rsa.c:945

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

934 {
935  struct GNUNET_JSON_Specification ret = {
936  .parser = &parse_rsa_signature,
937  .cleaner = &clean_rsa_signature,
938  .cls = NULL,
939  .field = name,
940  .ptr = sig,
941  .ptr_size = 0,
942  .size_ptr = NULL
943  };
944 
945  *sig = NULL;
946  return ret;
947 }
static void clean_rsa_signature(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA signature.
Definition: json_helper.c:918
static int parse_rsa_signature(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to RSA signature.
Definition: json_helper.c:871

◆ parse_boolean()

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

962 {
963  int *bp = spec->ptr;
964 
965  if (! json_is_boolean (root))
966  {
967  GNUNET_break_op (0);
968  return GNUNET_SYSERR;
969  }
970  *bp = json_boolean_value (root) ? GNUNET_YES : GNUNET_NO;
971  return GNUNET_OK;
972 }
@ 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 959 of file json_helper.c.

978 {
979  struct GNUNET_JSON_Specification ret = {
980  .parser = &parse_boolean,
981  .cleaner = NULL,
982  .cls = NULL,
983  .field = name,
984  .ptr = boolean,
985  .ptr_size = sizeof(int),
986  .size_ptr = NULL
987  };
988 
989  return ret;
990 }
static int 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:959

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