GNUnet debian-0.26.1
 
Loading...
Searching...
No Matches
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_util_lib.h"
27#include "gnunet_json_lib.h"
28
29json_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 ! spec[i].final;
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 if (NULL == spec[i].field_name)
59 GNUNET_assert (0 ==
60 json_object_update_new (ret,
61 spec[i].object));
62 else
63 GNUNET_assert (0 ==
64 json_object_set_new (ret,
65 spec[i].field_name,
66 spec[i].object));
67
68 spec[i].object = NULL;
69 }
70 }
71 return ret;
72}
73
74
77{
78 struct GNUNET_JSON_PackSpec ps = {
79 .final = true
80 };
81
82 return ps;
83}
84
85
88{
89 in.allow_null = true;
90 return in;
91}
92
93
96 bool b)
97{
98 struct GNUNET_JSON_PackSpec ps = {
99 .field_name = name,
100 .object = json_boolean (b)
101 };
102
103 GNUNET_assert (NULL != name);
104 return ps;
105}
106
107
110 double f)
111{
112 struct GNUNET_JSON_PackSpec ps = {
113 .field_name = name,
114 .object = json_real (f)
115 };
116
117 GNUNET_assert (NULL != name);
118 return ps;
119}
120
121
124 const char *s)
125{
126 struct GNUNET_JSON_PackSpec ps = {
127 .field_name = name,
128 .object = json_string (s)
129 };
130
131 GNUNET_assert (NULL != name);
132 return ps;
133}
134
135
138 uint64_t num)
139{
140 struct GNUNET_JSON_PackSpec ps = {
141 .field_name = name,
142 .object = json_integer ((json_int_t) num)
143 };
144
145 GNUNET_assert (NULL != name);
146#if JSON_INTEGER_IS_LONG_LONG
147 GNUNET_assert (num <= LLONG_MAX);
148#else
149 GNUNET_assert (num <= LONG_MAX);
150#endif
151 return ps;
152}
153
154
157 int64_t num)
158{
159 struct GNUNET_JSON_PackSpec ps = {
160 .field_name = name,
161 .object = json_integer ((json_int_t) num)
162 };
163
164 GNUNET_assert (NULL != name);
165#if JSON_INTEGER_IS_LONG_LONG
166 GNUNET_assert (num <= LLONG_MAX);
167 GNUNET_assert (num >= LLONG_MIN);
168#else
169 GNUNET_assert (num <= LONG_MAX);
170 GNUNET_assert (num >= LONG_MIN);
171#endif
172 return ps;
173}
174
175
178 json_t *o)
179{
180 struct GNUNET_JSON_PackSpec ps = {
181 .field_name = name,
182 .object = o
183 };
184
185 if (NULL == o)
186 return ps;
187 if (! json_is_object (o))
188 {
190 "Expected JSON object for field `%s'\n",
191 name);
192 GNUNET_assert (0);
193 }
194 return ps;
195}
196
197
200 json_t *o)
201{
202 struct GNUNET_JSON_PackSpec ps = {
203 .field_name = name,
204 .object = o
205 };
206
207 if (NULL == o)
208 return ps;
209 (void) json_incref (o);
210 if (! json_is_object (o))
211 {
213 "Expected JSON object for field `%s'\n",
214 name);
215 GNUNET_assert (0);
216 }
217 return ps;
218}
219
220
223 json_t *a)
224{
225 struct GNUNET_JSON_PackSpec ps = {
226 .field_name = name,
227 .object = a
228 };
229
230 GNUNET_assert (NULL != name);
231 if (NULL == a)
232 return ps;
233 if (! json_is_array (a))
234 {
236 "Expected JSON array for field `%s'\n",
237 name);
238 GNUNET_assert (0);
239 }
240 return ps;
241}
242
243
246 json_t *a)
247{
248 struct GNUNET_JSON_PackSpec ps = {
249 .field_name = name,
250 .object = a
251 };
252
253 GNUNET_assert (NULL != name);
254 if (NULL == a)
255 return ps;
256 (void) json_incref (a);
257 if (! json_is_array (a))
258 {
260 "Expected JSON array for field `%s'\n",
261 name);
262 GNUNET_assert (0);
263 }
264 return ps;
265}
266
267
270 const void *blob,
271 size_t blob_size)
272{
273 struct GNUNET_JSON_PackSpec ps = {
274 .field_name = name,
275 .object = (NULL != blob)
276 ? GNUNET_JSON_from_data (blob,
277 blob_size)
278 : NULL
279 };
280
281 GNUNET_assert (NULL != name);
282 return ps;
283}
284
285
288 const void *blob,
289 size_t blob_size)
290{
291 struct GNUNET_JSON_PackSpec ps = {
292 .field_name = name,
293 .object = (NULL != blob)
295 blob_size)
296 : NULL
297 };
298
299 GNUNET_assert (NULL != name);
300 return ps;
301}
302
303
307{
308 struct GNUNET_JSON_PackSpec ps = {
309 .field_name = name
310 };
311
312 GNUNET_assert (NULL != name);
313 if (! GNUNET_TIME_absolute_is_zero (t.abs_time))
314 {
316 GNUNET_assert (NULL != ps.object);
317 }
318 else
319 {
320 ps.object = NULL;
321 }
322 return ps;
323}
324
325
333
334
337 struct GNUNET_TIME_Relative rt)
338{
339 json_t *json;
340
341 GNUNET_assert (NULL != name);
342 json = GNUNET_JSON_from_time_rel (rt);
343 GNUNET_assert (NULL != json);
345 json);
346}
347
348
356
357
360 const struct GNUNET_CRYPTO_RsaPublicKey *pk)
361{
362 struct GNUNET_JSON_PackSpec ps = {
363 .field_name = name,
365 };
366
367 return ps;
368}
369
370
373 const struct GNUNET_CRYPTO_RsaSignature *sig)
374{
375 struct GNUNET_JSON_PackSpec ps = {
376 .field_name = name,
377 .object = GNUNET_JSON_from_rsa_signature (sig)
378 };
379
380 return ps;
381}
382
383
386 const struct
388{
389 struct GNUNET_JSON_PackSpec ps = {
390 .field_name = name
391 };
392
393 if (NULL == sig)
394 return ps;
395
396 switch (sig->cipher)
397 {
399 break;
401 ps.object = GNUNET_JSON_PACK (
402 GNUNET_JSON_pack_string ("cipher",
403 "RSA"),
404 GNUNET_JSON_pack_rsa_signature ("rsa_signature",
405 sig->details.rsa_signature));
406 return ps;
408 ps.object = GNUNET_JSON_PACK (
409 GNUNET_JSON_pack_string ("cipher",
410 "CS"),
411 GNUNET_JSON_pack_data_auto ("cs_signature_r",
412 &sig->details.cs_signature.r_point),
413 GNUNET_JSON_pack_data_auto ("cs_signature_s",
414 &sig->details.cs_signature.s_scalar));
415 return ps;
416 }
417 GNUNET_assert (0);
418 return ps;
419}
420
421
424 const char *name,
425 const struct GNUNET_CRYPTO_BlindedMessage *msg)
426{
427 struct GNUNET_JSON_PackSpec ps = {
428 .field_name = name,
429 };
430
431 switch (msg->cipher)
432 {
434 break;
436 ps.object = GNUNET_JSON_PACK (
437 GNUNET_JSON_pack_string ("cipher",
438 "RSA"),
440 "rsa_blinded_planchet",
441 msg->details.rsa_blinded_message.blinded_msg,
442 msg->details.rsa_blinded_message.blinded_msg_size));
443 return ps;
445 ps.object = GNUNET_JSON_PACK (
446 GNUNET_JSON_pack_string ("cipher",
447 "CS"),
449 "cs_nonce",
450 &msg->details.cs_blinded_message.nonce),
452 "cs_blinded_c0",
453 &msg->details.cs_blinded_message.c[0]),
455 "cs_blinded_c1",
456 &msg->details.cs_blinded_message.c[1]));
457 return ps;
458 }
459 GNUNET_assert (0);
460 return ps;
461}
462
463
466 const char *name,
467 const struct GNUNET_CRYPTO_BlindedSignature *sig)
468{
469 struct GNUNET_JSON_PackSpec ps = {
470 .field_name = name,
471 };
472
473 if (NULL == sig)
474 return ps;
475 switch (sig->cipher)
476 {
478 break;
480 ps.object = GNUNET_JSON_PACK (
481 GNUNET_JSON_pack_string ("cipher",
482 "RSA"),
483 GNUNET_JSON_pack_rsa_signature ("blinded_rsa_signature",
484 sig->details.blinded_rsa_signature));
485 return ps;
487 ps.object = GNUNET_JSON_PACK (
488 GNUNET_JSON_pack_string ("cipher",
489 "CS"),
491 sig->details.blinded_cs_answer.b),
493 &sig->details.blinded_cs_answer.s_scalar));
494 return ps;
495 }
496 GNUNET_assert (0);
497 return ps;
498}
499
500
504{
505 const char *str = "INVALID";
506
508 if (NULL == str)
509 {
510 GNUNET_break (0);
511 str = "INVALID";
512 }
514 str);
515}
516
517
518/* end of json_pack.c */
struct GNUNET_MessageHeader * msg
Definition 005.c:2
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
struct GNUNET_CRYPTO_BlindablePrivateKey pk
Private key from command line option, or NULL.
static char * name
Name (label) of the records to list.
static struct GNUNET_SCHEDULER_Task * t
Main task.
functions to parse JSON objects into GNUnet objects
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:336
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp(const char *name, struct GNUNET_TIME_Timestamp at)
Generate packer instruction for a JSON field of type timestamp.
Definition json_pack.c:305
json_t * GNUNET_JSON_from_timestamp(struct GNUNET_TIME_Timestamp stamp)
Convert timestamp to a json string.
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
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:177
json_t * GNUNET_JSON_from_data64(const void *data, size_t size)
Convert binary data to a JSON string with base64 encoding.
#define GNUNET_JSON_PACK(...)
Pack a JSON object from a spec.
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
#define GNUNET_JSON_pack_data_auto(name, blob)
Generate packer instruction for a JSON field where the size is automatically determined from the argu...
#define GNUNET_log(kind,...)
@ GNUNET_CRYPTO_BSA_INVALID
Invalid type of signature.
@ GNUNET_CRYPTO_BSA_CS
Clause Blind Schnorr signature.
@ GNUNET_CRYPTO_BSA_RSA
RSA blind signature.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ 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:626
GNUNET_TIME_RounderInterval
Quantities by which we support round up absolute time values.
bool GNUNET_TIME_absolute_is_zero(struct GNUNET_TIME_Absolute abs)
Test if abs is truly zero.
Definition time.c:844
const char * GNUNET_TIME_round_interval2s(enum GNUNET_TIME_RounderInterval ri)
Convert rounding interval to string.
Definition time.c:1293
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:336
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_(void)
Do not use directly.
Definition json_pack.c:76
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:222
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:269
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:95
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:305
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:177
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:245
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:87
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_unblinded_signature(const char *name, const struct GNUNET_CRYPTO_UnblindedSignature *sig)
Generate packer instruction for a JSON field of type unblinded signature.
Definition json_pack.c:385
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_double(const char *name, double f)
Generate packer instruction for a JSON field of type double.
Definition json_pack.c:109
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_blinded_message(const char *name, const struct GNUNET_CRYPTO_BlindedMessage *msg)
Generate packer instruction for a JSON field of type blinded message.
Definition json_pack.c:423
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:327
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_blinded_sig(const char *name, const struct GNUNET_CRYPTO_BlindedSignature *sig)
Generate packer instruction for a JSON field of type blinded signature.
Definition json_pack.c:465
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:137
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rounder_interval(const char *name, enum GNUNET_TIME_RounderInterval ri)
Generate packer instruction of a time rounder interval.
Definition json_pack.c:502
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:199
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:372
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:287
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:359
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:156
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:350
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:123
Blinded message ready for blind signing.
Type for blinded signatures.
The public information of an RSA key pair.
Definition crypto_rsa.c:53
Type of (unblinded) signatures.
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.
bool final
True if last element in the spec array.
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.
Time for timestamps used by GNUnet, in microseconds rounded to seconds.
enum GNUNET_TIME_RounderInterval ri
Definition time.c:1251
const char * str
Definition time.c:1252