GNUnet  0.20.0
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_data64 (const void *data, size_t size)
 Convert binary data to a JSON string with base64 encoding. More...
 
json_t * GNUNET_JSON_from_timestamp (struct GNUNET_TIME_Timestamp stamp)
 Convert timestamp to a json string. More...
 
json_t * GNUNET_JSON_from_timestamp_nbo (struct GNUNET_TIME_TimestampNBO stamp)
 Convert 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 31 of file json_generator.c.

33 {
34  char *buf;
35  json_t *json;
36 
37  if ((size * 8 + 4) / 5 + 1 >=
39  {
40  GNUNET_break (0);
41  return NULL;
42  }
44  size);
45  json = json_string (buf);
46  GNUNET_free (buf);
47  GNUNET_break (NULL != json);
48  return json;
49 }
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:763
static unsigned int size
Size of the "table".
Definition: peer.c:68

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_data64()

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

Convert binary data to a JSON string with base64 encoding.

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

Definition at line 53 of file json_generator.c.

55 {
56  char *buf = NULL;
57  json_t *json;
58  size_t len;
59 
60  if ((size * 8 + 5) / 6 + 1 >=
62  {
63  GNUNET_break (0);
64  return NULL;
65  }
67  size,
68  &buf);
69  if (NULL == buf)
70  {
71  GNUNET_break (0);
72  return NULL;
73  }
74  json = json_stringn (buf,
75  len);
76  GNUNET_free (buf);
77  GNUNET_break (NULL != json);
78  return json;
79 }
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1607

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

Here is the call graph for this function:

◆ GNUNET_JSON_from_timestamp()

json_t* GNUNET_JSON_from_timestamp ( struct GNUNET_TIME_Timestamp  stamp)

Convert timestamp to a json string.

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

Definition at line 83 of file json_generator.c.

84 {
85  json_t *j;
86 
87  j = json_object ();
88  if (NULL == j)
89  {
90  GNUNET_break (0);
91  return NULL;
92  }
94  {
95  if (0 !=
96  json_object_set_new (j,
97  "t_s",
98  json_string ("never")))
99  {
100  GNUNET_break (0);
101  json_decref (j);
102  return NULL;
103  }
104  return j;
105  }
106  GNUNET_assert (
107  0 ==
108  (stamp.abs_time.abs_value_us
109  % GNUNET_TIME_UNIT_SECONDS.rel_value_us));
110  if (0 !=
111  json_object_set_new (
112  j,
113  "t_s",
114  json_integer (
115  (json_int_t) (stamp.abs_time.abs_value_us
116  / GNUNET_TIME_UNIT_SECONDS.rel_value_us))))
117  {
118  GNUNET_break (0);
119  json_decref (j);
120  return NULL;
121  }
122  return j;
123 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_TIME_UNIT_SECONDS
One second.
bool GNUNET_TIME_absolute_is_never(struct GNUNET_TIME_Absolute abs)
Test if abs is never.
Definition: time.c:648
uint64_t abs_value_us
The actual value.
struct GNUNET_TIME_Absolute abs_time
The actual value.

References GNUNET_TIME_Timestamp::abs_time, GNUNET_TIME_Absolute::abs_value_us, GNUNET_assert, GNUNET_break, GNUNET_TIME_absolute_is_never(), and GNUNET_TIME_UNIT_SECONDS.

Referenced by GNUNET_JSON_from_timestamp_nbo().

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

◆ GNUNET_JSON_from_timestamp_nbo()

json_t* GNUNET_JSON_from_timestamp_nbo ( struct GNUNET_TIME_TimestampNBO  stamp)

Convert 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 {
130 }
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_ntoh(struct GNUNET_TIME_TimestampNBO tn)
Convert timestamp from network byte order.
Definition: time.c:101
json_t * GNUNET_JSON_from_timestamp(struct GNUNET_TIME_Timestamp stamp)
Convert timestamp to a json string.

References GNUNET_JSON_from_timestamp(), and GNUNET_TIME_timestamp_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 134 of file json_generator.c.

135 {
136  json_t *j;
137 
138  j = json_object ();
139  if (NULL == j)
140  {
141  GNUNET_break (0);
142  return NULL;
143  }
145  {
146  if (0 !=
147  json_object_set_new (j,
148  "d_us",
149  json_string ("forever")))
150  {
151  GNUNET_break (0);
152  json_decref (j);
153  return NULL;
154  }
155  return j;
156  }
157  if (stamp.rel_value_us >= (1LLU << 53))
158  {
159  /* value is larger than allowed */
160  GNUNET_break (0);
161  return NULL;
162  }
163  if (0 !=
164  json_object_set_new (
165  j,
166  "d_us",
167  json_integer ((json_int_t) stamp.rel_value_us)))
168  {
169  GNUNET_break (0);
170  json_decref (j);
171  return NULL;
172  }
173  return j;
174 }
bool GNUNET_TIME_relative_is_forever(struct GNUNET_TIME_Relative rel)
Test if rel is forever.
Definition: time.c:655
uint64_t rel_value_us
The actual value.

References GNUNET_break, GNUNET_TIME_relative_is_forever(), 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 178 of file json_generator.c.

179 {
180  void *buf;
181  size_t buf_len;
182  json_t *ret;
183 
185  &buf);
187  buf_len);
188  GNUNET_free (buf);
189  return ret;
190 }
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:325
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 194 of file json_generator.c.

195 {
196  void *buf;
197  size_t buf_len;
198  json_t *ret;
199 
200  buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
201  &buf);
203  buf_len);
204  GNUNET_free (buf);
205  return ret;
206 }
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:999

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

Here is the call graph for this function: