diff options
Diffstat (limited to 'src/lib/json/json_pack.c')
-rw-r--r-- | src/lib/json/json_pack.c | 367 |
1 files changed, 367 insertions, 0 deletions
diff --git a/src/lib/json/json_pack.c b/src/lib/json/json_pack.c new file mode 100644 index 000000000..18487c3f4 --- /dev/null +++ b/src/lib/json/json_pack.c | |||
@@ -0,0 +1,367 @@ | |||
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 | */ | ||
20 | /** | ||
21 | * @file json/json_pack.c | ||
22 | * @brief functions to pack JSON objects | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_json_lib.h" | ||
27 | |||
28 | |||
29 | json_t * | ||
30 | GNUNET_JSON_pack_ (struct GNUNET_JSON_PackSpec spec[]) | ||
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 | { | ||
50 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
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 | |||
69 | struct GNUNET_JSON_PackSpec | ||
70 | GNUNET_JSON_pack_end_ (void) | ||
71 | { | ||
72 | struct GNUNET_JSON_PackSpec ps = { | ||
73 | .field_name = NULL | ||
74 | }; | ||
75 | |||
76 | return ps; | ||
77 | } | ||
78 | |||
79 | |||
80 | struct GNUNET_JSON_PackSpec | ||
81 | GNUNET_JSON_pack_allow_null (struct GNUNET_JSON_PackSpec in) | ||
82 | { | ||
83 | in.allow_null = true; | ||
84 | return in; | ||
85 | } | ||
86 | |||
87 | |||
88 | struct GNUNET_JSON_PackSpec | ||
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 | |||
101 | struct GNUNET_JSON_PackSpec | ||
102 | GNUNET_JSON_pack_double (const char *name, | ||
103 | double f) | ||
104 | { | ||
105 | struct GNUNET_JSON_PackSpec ps = { | ||
106 | .field_name = name, | ||
107 | .object = json_real (f) | ||
108 | }; | ||
109 | |||
110 | return ps; | ||
111 | } | ||
112 | |||
113 | |||
114 | struct GNUNET_JSON_PackSpec | ||
115 | GNUNET_JSON_pack_string (const char *name, | ||
116 | const char *s) | ||
117 | { | ||
118 | struct GNUNET_JSON_PackSpec ps = { | ||
119 | .field_name = name, | ||
120 | .object = json_string (s) | ||
121 | }; | ||
122 | |||
123 | return ps; | ||
124 | } | ||
125 | |||
126 | |||
127 | struct GNUNET_JSON_PackSpec | ||
128 | GNUNET_JSON_pack_uint64 (const char *name, | ||
129 | uint64_t num) | ||
130 | { | ||
131 | struct GNUNET_JSON_PackSpec ps = { | ||
132 | .field_name = name, | ||
133 | .object = json_integer ((json_int_t) num) | ||
134 | }; | ||
135 | |||
136 | #if JSON_INTEGER_IS_LONG_LONG | ||
137 | GNUNET_assert (num <= LLONG_MAX); | ||
138 | #else | ||
139 | GNUNET_assert (num <= LONG_MAX); | ||
140 | #endif | ||
141 | return ps; | ||
142 | } | ||
143 | |||
144 | |||
145 | struct GNUNET_JSON_PackSpec | ||
146 | GNUNET_JSON_pack_int64 (const char *name, | ||
147 | int64_t num) | ||
148 | { | ||
149 | struct GNUNET_JSON_PackSpec ps = { | ||
150 | .field_name = name, | ||
151 | .object = json_integer ((json_int_t) num) | ||
152 | }; | ||
153 | |||
154 | #if JSON_INTEGER_IS_LONG_LONG | ||
155 | GNUNET_assert (num <= LLONG_MAX); | ||
156 | GNUNET_assert (num >= LLONG_MIN); | ||
157 | #else | ||
158 | GNUNET_assert (num <= LONG_MAX); | ||
159 | GNUNET_assert (num >= LONG_MIN); | ||
160 | #endif | ||
161 | return ps; | ||
162 | } | ||
163 | |||
164 | |||
165 | struct GNUNET_JSON_PackSpec | ||
166 | GNUNET_JSON_pack_object_steal (const char *name, | ||
167 | json_t *o) | ||
168 | { | ||
169 | struct GNUNET_JSON_PackSpec ps = { | ||
170 | .field_name = name, | ||
171 | .object = o | ||
172 | }; | ||
173 | |||
174 | if (NULL == o) | ||
175 | return ps; | ||
176 | if (! json_is_object (o)) | ||
177 | { | ||
178 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
179 | "Expected JSON object for field `%s'\n", | ||
180 | name); | ||
181 | GNUNET_assert (0); | ||
182 | } | ||
183 | return ps; | ||
184 | } | ||
185 | |||
186 | |||
187 | struct GNUNET_JSON_PackSpec | ||
188 | GNUNET_JSON_pack_object_incref (const char *name, | ||
189 | json_t *o) | ||
190 | { | ||
191 | struct GNUNET_JSON_PackSpec ps = { | ||
192 | .field_name = name, | ||
193 | .object = o | ||
194 | }; | ||
195 | |||
196 | if (NULL == o) | ||
197 | return ps; | ||
198 | (void) json_incref (o); | ||
199 | if (! json_is_object (o)) | ||
200 | { | ||
201 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
202 | "Expected JSON object for field `%s'\n", | ||
203 | name); | ||
204 | GNUNET_assert (0); | ||
205 | } | ||
206 | return ps; | ||
207 | } | ||
208 | |||
209 | |||
210 | struct GNUNET_JSON_PackSpec | ||
211 | GNUNET_JSON_pack_array_steal (const char *name, | ||
212 | json_t *a) | ||
213 | { | ||
214 | struct GNUNET_JSON_PackSpec ps = { | ||
215 | .field_name = name, | ||
216 | .object = a | ||
217 | }; | ||
218 | |||
219 | if (NULL == a) | ||
220 | return ps; | ||
221 | if (! json_is_array (a)) | ||
222 | { | ||
223 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
224 | "Expected JSON array for field `%s'\n", | ||
225 | name); | ||
226 | GNUNET_assert (0); | ||
227 | } | ||
228 | return ps; | ||
229 | } | ||
230 | |||
231 | |||
232 | struct GNUNET_JSON_PackSpec | ||
233 | GNUNET_JSON_pack_array_incref (const char *name, | ||
234 | json_t *a) | ||
235 | { | ||
236 | struct GNUNET_JSON_PackSpec ps = { | ||
237 | .field_name = name, | ||
238 | .object = a | ||
239 | }; | ||
240 | |||
241 | if (NULL == a) | ||
242 | return ps; | ||
243 | (void) json_incref (a); | ||
244 | if (! json_is_array (a)) | ||
245 | { | ||
246 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
247 | "Expected JSON array for field `%s'\n", | ||
248 | name); | ||
249 | GNUNET_assert (0); | ||
250 | } | ||
251 | return ps; | ||
252 | } | ||
253 | |||
254 | |||
255 | struct GNUNET_JSON_PackSpec | ||
256 | GNUNET_JSON_pack_data_varsize (const char *name, | ||
257 | const void *blob, | ||
258 | size_t blob_size) | ||
259 | { | ||
260 | struct GNUNET_JSON_PackSpec ps = { | ||
261 | .field_name = name, | ||
262 | .object = (NULL != blob) | ||
263 | ? GNUNET_JSON_from_data (blob, | ||
264 | blob_size) | ||
265 | : NULL | ||
266 | }; | ||
267 | |||
268 | return ps; | ||
269 | } | ||
270 | |||
271 | |||
272 | struct GNUNET_JSON_PackSpec | ||
273 | GNUNET_JSON_pack_data64_varsize (const char *name, | ||
274 | const void *blob, | ||
275 | size_t blob_size) | ||
276 | { | ||
277 | struct GNUNET_JSON_PackSpec ps = { | ||
278 | .field_name = name, | ||
279 | .object = (NULL != blob) | ||
280 | ? GNUNET_JSON_from_data64 (blob, | ||
281 | blob_size) | ||
282 | : NULL | ||
283 | }; | ||
284 | |||
285 | return ps; | ||
286 | } | ||
287 | |||
288 | |||
289 | struct GNUNET_JSON_PackSpec | ||
290 | GNUNET_JSON_pack_timestamp (const char *name, | ||
291 | struct GNUNET_TIME_Timestamp t) | ||
292 | { | ||
293 | struct GNUNET_JSON_PackSpec ps = { | ||
294 | .field_name = name | ||
295 | }; | ||
296 | |||
297 | if (! GNUNET_TIME_absolute_is_zero (t.abs_time)) | ||
298 | { | ||
299 | ps.object = GNUNET_JSON_from_timestamp (t); | ||
300 | GNUNET_assert (NULL != ps.object); | ||
301 | } | ||
302 | else | ||
303 | { | ||
304 | ps.object = NULL; | ||
305 | } | ||
306 | return ps; | ||
307 | } | ||
308 | |||
309 | |||
310 | struct GNUNET_JSON_PackSpec | ||
311 | GNUNET_JSON_pack_timestamp_nbo (const char *name, | ||
312 | struct GNUNET_TIME_TimestampNBO at) | ||
313 | { | ||
314 | return GNUNET_JSON_pack_timestamp (name, | ||
315 | GNUNET_TIME_timestamp_ntoh (at)); | ||
316 | } | ||
317 | |||
318 | |||
319 | struct GNUNET_JSON_PackSpec | ||
320 | GNUNET_JSON_pack_time_rel (const char *name, | ||
321 | struct GNUNET_TIME_Relative rt) | ||
322 | { | ||
323 | json_t *json; | ||
324 | |||
325 | json = GNUNET_JSON_from_time_rel (rt); | ||
326 | GNUNET_assert (NULL != json); | ||
327 | return GNUNET_JSON_pack_object_steal (name, | ||
328 | json); | ||
329 | } | ||
330 | |||
331 | |||
332 | struct GNUNET_JSON_PackSpec | ||
333 | GNUNET_JSON_pack_time_rel_nbo (const char *name, | ||
334 | struct GNUNET_TIME_RelativeNBO rt) | ||
335 | { | ||
336 | return GNUNET_JSON_pack_time_rel (name, | ||
337 | GNUNET_TIME_relative_ntoh (rt)); | ||
338 | } | ||
339 | |||
340 | |||
341 | struct GNUNET_JSON_PackSpec | ||
342 | GNUNET_JSON_pack_rsa_public_key (const char *name, | ||
343 | const struct GNUNET_CRYPTO_RsaPublicKey *pk) | ||
344 | { | ||
345 | struct GNUNET_JSON_PackSpec ps = { | ||
346 | .field_name = name, | ||
347 | .object = GNUNET_JSON_from_rsa_public_key (pk) | ||
348 | }; | ||
349 | |||
350 | return ps; | ||
351 | } | ||
352 | |||
353 | |||
354 | struct GNUNET_JSON_PackSpec | ||
355 | GNUNET_JSON_pack_rsa_signature (const char *name, | ||
356 | const struct GNUNET_CRYPTO_RsaSignature *sig) | ||
357 | { | ||
358 | struct GNUNET_JSON_PackSpec ps = { | ||
359 | .field_name = name, | ||
360 | .object = GNUNET_JSON_from_rsa_signature (sig) | ||
361 | }; | ||
362 | |||
363 | return ps; | ||
364 | } | ||
365 | |||
366 | |||
367 | /* end of json_pack.c */ | ||