GNUnet  0.11.x
Functions
json_generator.c File Reference

helper functions for generating JSON from GNUnet data structures More...

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

Go to the source code of this file.

Functions

json_t * GNUNET_JSON_from_data (const void *data, size_t size)
 Convert binary data to a JSON string with the base32crockford encoding. More...
 
json_t * GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
 Convert absolute timestamp to a json string. More...
 
json_t * GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
 Convert absolute timestamp to a json string. More...
 
json_t * GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
 Convert relative timestamp to a json string. More...
 
json_t * GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
 Convert RSA public key to JSON. More...
 
json_t * GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
 Convert RSA signature to JSON. More...
 

Detailed Description

helper functions for generating JSON from GNUnet data structures

Author
Sree Harsha Totakura sreeh.nosp@m.arsh.nosp@m.a@tot.nosp@m.akur.nosp@m.a.in

Definition in file json_generator.c.

Function Documentation

◆ GNUNET_JSON_from_data()

json_t* GNUNET_JSON_from_data ( const void *  data,
size_t  size 
)

Convert binary data to a JSON string with the base32crockford encoding.

Parameters
databinary data
sizesize of data in bytes
Returns
json string that encodes data

Definition at line 39 of file json_generator.c.

41 {
42  char *buf;
43  json_t *json;
44 
45  if ((size * 8 + 4) / 5 + 1 >=
47  {
48  GNUNET_break (0);
49  return NULL;
50  }
52  size);
53  json = json_string (buf);
54  GNUNET_free (buf);
55  GNUNET_break (NULL != json);
56  return json;
57 }
uint32_t data
The data value.
static char buf[2048]
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:750
static unsigned int size
Size of the "table".
Definition: peer.c:67

References buf, data, GNUNET_break, GNUNET_free, GNUNET_MAX_MALLOC_CHECKED, GNUNET_STRINGS_data_to_string_alloc(), and size.

Referenced by GNUNET_JSON_from_rsa_public_key(), and GNUNET_JSON_from_rsa_signature().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_JSON_from_time_abs()

json_t* GNUNET_JSON_from_time_abs ( struct GNUNET_TIME_Absolute  stamp)

Convert absolute timestamp to a json string.

Parameters
stampthe time stamp
Returns
a json string with the timestamp in stamp

Definition at line 67 of file json_generator.c.

68 {
69  json_t *j;
70 
72  GNUNET_TIME_round_abs (&stamp));
73 
74  j = json_object ();
75  if (NULL == j)
76  {
77  GNUNET_break (0);
78  return NULL;
79  }
80  if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
81  {
82  if (0 !=
83  json_object_set_new (j,
84  "t_ms",
85  json_string ("never")))
86  {
87  GNUNET_break (0);
88  json_decref (j);
89  return NULL;
90  }
91  return j;
92  }
93  if (0 !=
94  json_object_set_new (j,
95  "t_ms",
96  json_integer ((json_int_t) (stamp.abs_value_us
97  / 1000LL))))
98  {
99  GNUNET_break (0);
100  json_decref (j);
101  return NULL;
102  }
103  return j;
104 }
@ GNUNET_OK
Definition: gnunet_common.h:95
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int GNUNET_TIME_round_abs(struct GNUNET_TIME_Absolute *at)
Round a time value so that it is suitable for transmission via JSON encodings.
Definition: time.c:62
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
uint64_t abs_value_us
The actual value.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_assert, GNUNET_break, GNUNET_OK, GNUNET_TIME_round_abs(), and GNUNET_TIME_UNIT_FOREVER_ABS.

Referenced by GNUNET_JSON_from_time_abs_nbo().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_JSON_from_time_abs_nbo()

json_t* GNUNET_JSON_from_time_abs_nbo ( struct GNUNET_TIME_AbsoluteNBO  stamp)

Convert absolute timestamp to a json string.

Parameters
stampthe time stamp
Returns
a json string with the timestamp in stamp

Definition at line 114 of file json_generator.c.

115 {
117 }
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:542
json_t * GNUNET_JSON_from_time_abs(struct GNUNET_TIME_Absolute stamp)
Convert absolute timestamp to a json string.

References GNUNET_JSON_from_time_abs(), and GNUNET_TIME_absolute_ntoh().

Here is the call graph for this function:

◆ GNUNET_JSON_from_time_rel()

json_t* GNUNET_JSON_from_time_rel ( struct GNUNET_TIME_Relative  stamp)

Convert relative timestamp to a json string.

Parameters
stampthe time stamp
Returns
a json string with the timestamp in stamp

Definition at line 127 of file json_generator.c.

128 {
129  json_t *j;
130 
132  GNUNET_TIME_round_rel (&stamp));
133 
134  j = json_object ();
135  if (NULL == j)
136  {
137  GNUNET_break (0);
138  return NULL;
139  }
140  if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
141  {
142  if (0 !=
143  json_object_set_new (j,
144  "d_ms",
145  json_string ("forever")))
146  {
147  GNUNET_break (0);
148  json_decref (j);
149  return NULL;
150  }
151  return j;
152  }
153  if (0 !=
154  json_object_set_new (j,
155  "d_ms",
156  json_integer ((json_int_t) (stamp.rel_value_us
157  / 1000LL))))
158  {
159  GNUNET_break (0);
160  json_decref (j);
161  return NULL;
162  }
163  return j;
164 }
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
int GNUNET_TIME_round_rel(struct GNUNET_TIME_Relative *rt)
Round a time value so that it is suitable for transmission via JSON encodings.
Definition: time.c:74
uint64_t rel_value_us
The actual value.

References GNUNET_assert, GNUNET_break, GNUNET_OK, GNUNET_TIME_round_rel(), GNUNET_TIME_UNIT_FOREVER_REL, and GNUNET_TIME_Relative::rel_value_us.

Here is the call graph for this function:

◆ GNUNET_JSON_from_rsa_public_key()

json_t* GNUNET_JSON_from_rsa_public_key ( const struct GNUNET_CRYPTO_RsaPublicKey pk)

Convert RSA public key to JSON.

Parameters
pkpublic key to convert
Returns
corresponding JSON encoding

Definition at line 174 of file json_generator.c.

175 {
176  void *buf;
177  size_t buf_len;
178  json_t *ret;
179 
181  &buf);
183  buf_len);
184  GNUNET_free (buf);
185  return ret;
186 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
size_t GNUNET_CRYPTO_rsa_public_key_encode(const struct GNUNET_CRYPTO_RsaPublicKey *key, void **buffer)
Encode the public key in a format suitable for storing it into a file.
Definition: crypto_rsa.c:300
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.

References buf, GNUNET_CRYPTO_rsa_public_key_encode(), GNUNET_free, GNUNET_JSON_from_data(), pk, and ret.

Here is the call graph for this function:

◆ GNUNET_JSON_from_rsa_signature()

json_t* GNUNET_JSON_from_rsa_signature ( const struct GNUNET_CRYPTO_RsaSignature sig)

Convert RSA signature to JSON.

Parameters
sigsignature to convert
Returns
corresponding JSON encoding

Definition at line 196 of file json_generator.c.

197 {
198  void *buf;
199  size_t buf_len;
200  json_t *ret;
201 
202  buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
203  &buf);
205  buf_len);
206  GNUNET_free (buf);
207  return ret;
208 }
size_t GNUNET_CRYPTO_rsa_signature_encode(const struct GNUNET_CRYPTO_RsaSignature *sig, void **buffer)
Encode the given signature in a format suitable for storing it into a file.
Definition: crypto_rsa.c:953

References buf, GNUNET_CRYPTO_rsa_signature_encode(), GNUNET_free, GNUNET_JSON_from_data(), and ret.

Here is the call graph for this function: