GNUnet  0.20.0
json_pack.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2021 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
25 #include "platform.h"
26 #include "gnunet_json_lib.h"
27 
28 
29 json_t *
31 {
32  json_t *ret;
33 
34  if (NULL == spec[0].field_name)
35  {
36  ret = spec[0].object;
37  spec[0].object = NULL;
38  return ret;
39  }
40  ret = json_object ();
41  GNUNET_assert (NULL != ret);
42  for (unsigned int i = 0;
43  NULL != spec[i].field_name;
44  i++)
45  {
46  if (NULL == spec[i].object)
47  {
48  if (! spec[i].allow_null)
49  {
51  "NULL not allowed for `%s'\n",
52  spec[i].field_name);
53  GNUNET_assert (0);
54  }
55  }
56  else
57  {
58  GNUNET_assert (0 ==
59  json_object_set_new (ret,
60  spec[i].field_name,
61  spec[i].object));
62  spec[i].object = NULL;
63  }
64  }
65  return ret;
66 }
67 
68 
71 {
72  struct GNUNET_JSON_PackSpec ps = {
73  .field_name = NULL
74  };
75 
76  return ps;
77 }
78 
79 
82 {
83  in.allow_null = true;
84  return in;
85 }
86 
87 
89 GNUNET_JSON_pack_bool (const char *name,
90  bool b)
91 {
92  struct GNUNET_JSON_PackSpec ps = {
93  .field_name = name,
94  .object = json_boolean (b)
95  };
96 
97  return ps;
98 }
99 
100 
102 GNUNET_JSON_pack_string (const char *name,
103  const char *s)
104 {
105  struct GNUNET_JSON_PackSpec ps = {
106  .field_name = name,
107  .object = json_string (s)
108  };
109 
110  return ps;
111 }
112 
113 
115 GNUNET_JSON_pack_uint64 (const char *name,
116  uint64_t num)
117 {
118  struct GNUNET_JSON_PackSpec ps = {
119  .field_name = name,
120  .object = json_integer ((json_int_t) num)
121  };
122 
123 #if JSON_INTEGER_IS_LONG_LONG
124  GNUNET_assert (num <= LLONG_MAX);
125 #else
126  GNUNET_assert (num <= LONG_MAX);
127 #endif
128  return ps;
129 }
130 
131 
133 GNUNET_JSON_pack_int64 (const char *name,
134  int64_t num)
135 {
136  struct GNUNET_JSON_PackSpec ps = {
137  .field_name = name,
138  .object = json_integer ((json_int_t) num)
139  };
140 
141 #if JSON_INTEGER_IS_LONG_LONG
142  GNUNET_assert (num <= LLONG_MAX);
143  GNUNET_assert (num >= LLONG_MIN);
144 #else
145  GNUNET_assert (num <= LONG_MAX);
146  GNUNET_assert (num >= LONG_MIN);
147 #endif
148  return ps;
149 }
150 
151 
154  json_t *o)
155 {
156  struct GNUNET_JSON_PackSpec ps = {
157  .field_name = name,
158  .object = o
159  };
160 
161  if (NULL == o)
162  return ps;
163  if (! json_is_object (o))
164  {
166  "Expected JSON object for field `%s'\n",
167  name);
168  GNUNET_assert (0);
169  }
170  return ps;
171 }
172 
173 
176  json_t *o)
177 {
178  struct GNUNET_JSON_PackSpec ps = {
179  .field_name = name,
180  .object = o
181  };
182 
183  if (NULL == o)
184  return ps;
185  (void) json_incref (o);
186  if (! json_is_object (o))
187  {
189  "Expected JSON object for field `%s'\n",
190  name);
191  GNUNET_assert (0);
192  }
193  return ps;
194 }
195 
196 
199  json_t *a)
200 {
201  struct GNUNET_JSON_PackSpec ps = {
202  .field_name = name,
203  .object = a
204  };
205 
206  if (NULL == a)
207  return ps;
208  if (! json_is_array (a))
209  {
211  "Expected JSON array for field `%s'\n",
212  name);
213  GNUNET_assert (0);
214  }
215  return ps;
216 }
217 
218 
221  json_t *a)
222 {
223  struct GNUNET_JSON_PackSpec ps = {
224  .field_name = name,
225  .object = a
226  };
227 
228  if (NULL == a)
229  return ps;
230  (void) json_incref (a);
231  if (! json_is_array (a))
232  {
234  "Expected JSON array for field `%s'\n",
235  name);
236  GNUNET_assert (0);
237  }
238  return ps;
239 }
240 
241 
244  const void *blob,
245  size_t blob_size)
246 {
247  struct GNUNET_JSON_PackSpec ps = {
248  .field_name = name,
249  .object = (NULL != blob)
250  ? GNUNET_JSON_from_data (blob,
251  blob_size)
252  : NULL
253  };
254 
255  return ps;
256 }
257 
258 
261  const void *blob,
262  size_t blob_size)
263 {
264  struct GNUNET_JSON_PackSpec ps = {
265  .field_name = name,
266  .object = (NULL != blob)
267  ? GNUNET_JSON_from_data64 (blob,
268  blob_size)
269  : NULL
270  };
271 
272  return ps;
273 }
274 
275 
277 GNUNET_JSON_pack_timestamp (const char *name,
278  struct GNUNET_TIME_Timestamp t)
279 {
280  struct GNUNET_JSON_PackSpec ps = {
281  .field_name = name
282  };
283 
284  if (! GNUNET_TIME_absolute_is_zero (t.abs_time))
285  {
287  GNUNET_assert (NULL != ps.object);
288  }
289  else
290  {
291  ps.object = NULL;
292  }
293  return ps;
294 }
295 
296 
299  struct GNUNET_TIME_TimestampNBO at)
300 {
303 }
304 
305 
307 GNUNET_JSON_pack_time_rel (const char *name,
308  struct GNUNET_TIME_Relative rt)
309 {
310  json_t *json;
311 
312  json = GNUNET_JSON_from_time_rel (rt);
313  GNUNET_assert (NULL != json);
315  json);
316 }
317 
318 
321  struct GNUNET_TIME_RelativeNBO rt)
322 {
325 }
326 
327 
330  const struct GNUNET_CRYPTO_RsaPublicKey *pk)
331 {
332  struct GNUNET_JSON_PackSpec ps = {
333  .field_name = name,
335  };
336 
337  return ps;
338 }
339 
340 
343  const struct GNUNET_CRYPTO_RsaSignature *sig)
344 {
345  struct GNUNET_JSON_PackSpec ps = {
346  .field_name = name,
347  .object = GNUNET_JSON_from_rsa_signature (sig)
348  };
349 
350  return ps;
351 }
352 
353 
354 /* end of json_pack.c */
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.
static struct GNUNET_SCHEDULER_Task * t
Main task.
functions to parse JSON objects into GNUnet objects
json_t * GNUNET_JSON_from_timestamp(struct GNUNET_TIME_Timestamp stamp)
Convert timestamp to a json string.
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
json_t * GNUNET_JSON_from_data64(const void *data, size_t size)
Convert binary data to a JSON string with base64 encoding.
#define GNUNET_log(kind,...)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
Definition: time.c:628
bool GNUNET_TIME_absolute_is_zero(struct GNUNET_TIME_Absolute abs)
Test if abs is truly zero.
Definition: time.c:844
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_ntoh(struct GNUNET_TIME_TimestampNBO tn)
Convert timestamp from network byte order.
Definition: time.c:101
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel(const char *name, struct GNUNET_TIME_Relative rt)
Generate packer instruction for a JSON field of type relative time.
Definition: json_pack.c:307
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_(void)
Do not use directly.
Definition: json_pack.c:70
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_steal(const char *name, json_t *a)
Generate packer instruction for a JSON field of type JSON array where the reference is taken over by ...
Definition: json_pack.c:198
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data_varsize(const char *name, const void *blob, size_t blob_size)
Generate packer instruction for a JSON field of type variable size binary blob.
Definition: json_pack.c:243
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_bool(const char *name, bool b)
Generate packer instruction for a JSON field of type bool.
Definition: json_pack.c:89
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp(const char *name, struct GNUNET_TIME_Timestamp t)
Generate packer instruction for a JSON field of type timestamp.
Definition: json_pack.c:277
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_steal(const char *name, json_t *o)
Generate packer instruction for a JSON field of type JSON object where the reference is taken over by...
Definition: json_pack.c:153
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_incref(const char *name, json_t *a)
Generate packer instruction for a JSON field of type JSON array where the reference counter is increm...
Definition: json_pack.c:220
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_allow_null(struct GNUNET_JSON_PackSpec in)
Modify packer instruction to allow NULL as a value.
Definition: json_pack.c:81
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp_nbo(const char *name, struct GNUNET_TIME_TimestampNBO at)
Generate packer instruction for a JSON field of type timestamp in network byte order.
Definition: json_pack.c:298
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_uint64(const char *name, uint64_t num)
Generate packer instruction for a JSON field of type unsigned integer.
Definition: json_pack.c:115
json_t * GNUNET_JSON_pack_(struct GNUNET_JSON_PackSpec spec[])
Pack a JSON object from a spec.
Definition: json_pack.c:30
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_incref(const char *name, json_t *o)
Generate packer instruction for a JSON field of type JSON object where the reference counter is incre...
Definition: json_pack.c:175
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_signature(const char *name, const struct GNUNET_CRYPTO_RsaSignature *sig)
Generate packer instruction for a JSON field of type RSA signature.
Definition: json_pack.c:342
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data64_varsize(const char *name, const void *blob, size_t blob_size)
Generate packer instruction for a JSON field of type variable size binary blob.
Definition: json_pack.c:260
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_public_key(const char *name, const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Generate packer instruction for a JSON field of type RSA public key.
Definition: json_pack.c:329
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_int64(const char *name, int64_t num)
Generate packer instruction for a JSON field of type signed integer.
Definition: json_pack.c:133
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel_nbo(const char *name, struct GNUNET_TIME_RelativeNBO rt)
Generate packer instruction for a JSON field of type relative time in network byte order.
Definition: json_pack.c:320
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_string(const char *name, const char *s)
Generate packer instruction for a JSON field of type string.
Definition: json_pack.c:102
const char * name
The public information of an RSA key pair.
Definition: crypto_rsa.c:53
an RSA signature
Definition: crypto_rsa.c:65
Element in the array to give to the packer.
bool allow_null
True if a NULL (or 0) argument is allowed.
json_t * object
Object to pack.
const char * field_name
Name of the field to pack.
Time for relative time used by GNUnet, in microseconds and in network byte order.
Time for relative time used by GNUnet, in microseconds.
Time for timestamps used by GNUnet, in seconds and in network byte order.
Rounded time for timestamps used by GNUnet, in seconds.