diff options
Diffstat (limited to 'src/json/json_pack.c')
-rw-r--r-- | src/json/json_pack.c | 331 |
1 files changed, 0 insertions, 331 deletions
diff --git a/src/json/json_pack.c b/src/json/json_pack.c deleted file mode 100644 index cc1ca3e97..000000000 --- a/src/json/json_pack.c +++ /dev/null | |||
@@ -1,331 +0,0 @@ | |||
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 | GNUNET_assert (spec[i].allow_null); | ||
49 | } | ||
50 | else | ||
51 | { | ||
52 | GNUNET_assert (0 == | ||
53 | json_object_set_new (ret, | ||
54 | spec[i].field_name, | ||
55 | spec[i].object)); | ||
56 | spec[i].object = NULL; | ||
57 | } | ||
58 | } | ||
59 | return ret; | ||
60 | } | ||
61 | |||
62 | |||
63 | struct GNUNET_JSON_PackSpec | ||
64 | GNUNET_JSON_pack_end_ (void) | ||
65 | { | ||
66 | struct GNUNET_JSON_PackSpec ps = { | ||
67 | .field_name = NULL | ||
68 | }; | ||
69 | |||
70 | return ps; | ||
71 | } | ||
72 | |||
73 | |||
74 | struct GNUNET_JSON_PackSpec | ||
75 | GNUNET_JSON_pack_allow_null (struct GNUNET_JSON_PackSpec in) | ||
76 | { | ||
77 | in.allow_null = true; | ||
78 | return in; | ||
79 | } | ||
80 | |||
81 | |||
82 | struct GNUNET_JSON_PackSpec | ||
83 | GNUNET_JSON_pack_bool (const char *name, | ||
84 | bool b) | ||
85 | { | ||
86 | struct GNUNET_JSON_PackSpec ps = { | ||
87 | .field_name = name, | ||
88 | .object = json_boolean (b) | ||
89 | }; | ||
90 | |||
91 | return ps; | ||
92 | } | ||
93 | |||
94 | |||
95 | struct GNUNET_JSON_PackSpec | ||
96 | GNUNET_JSON_pack_string (const char *name, | ||
97 | const char *s) | ||
98 | { | ||
99 | struct GNUNET_JSON_PackSpec ps = { | ||
100 | .field_name = name, | ||
101 | .object = json_string (s) | ||
102 | }; | ||
103 | |||
104 | return ps; | ||
105 | } | ||
106 | |||
107 | |||
108 | struct GNUNET_JSON_PackSpec | ||
109 | GNUNET_JSON_pack_uint64 (const char *name, | ||
110 | uint64_t num) | ||
111 | { | ||
112 | struct GNUNET_JSON_PackSpec ps = { | ||
113 | .field_name = name, | ||
114 | .object = json_integer ((json_int_t) num) | ||
115 | }; | ||
116 | |||
117 | #if JSON_INTEGER_IS_LONG_LONG | ||
118 | GNUNET_assert (num <= LLONG_MAX); | ||
119 | #else | ||
120 | GNUNET_assert (num <= LONG_MAX); | ||
121 | #endif | ||
122 | return ps; | ||
123 | } | ||
124 | |||
125 | |||
126 | struct GNUNET_JSON_PackSpec | ||
127 | GNUNET_JSON_pack_int64 (const char *name, | ||
128 | int64_t num) | ||
129 | { | ||
130 | struct GNUNET_JSON_PackSpec ps = { | ||
131 | .field_name = name, | ||
132 | .object = json_integer ((json_int_t) num) | ||
133 | }; | ||
134 | |||
135 | #if JSON_INTEGER_IS_LONG_LONG | ||
136 | GNUNET_assert (num <= LLONG_MAX); | ||
137 | GNUNET_assert (num >= LLONG_MIN); | ||
138 | #else | ||
139 | GNUNET_assert (num <= LONG_MAX); | ||
140 | GNUNET_assert (num >= LONG_MIN); | ||
141 | #endif | ||
142 | return ps; | ||
143 | } | ||
144 | |||
145 | |||
146 | struct GNUNET_JSON_PackSpec | ||
147 | GNUNET_JSON_pack_object_steal (const char *name, | ||
148 | json_t *o) | ||
149 | { | ||
150 | struct GNUNET_JSON_PackSpec ps = { | ||
151 | .field_name = name, | ||
152 | .object = o | ||
153 | }; | ||
154 | |||
155 | if (NULL == o) | ||
156 | return ps; | ||
157 | if (! json_is_object (o)) | ||
158 | { | ||
159 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
160 | "Expected JSON object for field `%s'\n", | ||
161 | name); | ||
162 | GNUNET_assert (0); | ||
163 | } | ||
164 | return ps; | ||
165 | } | ||
166 | |||
167 | |||
168 | struct GNUNET_JSON_PackSpec | ||
169 | GNUNET_JSON_pack_object_incref (const char *name, | ||
170 | json_t *o) | ||
171 | { | ||
172 | struct GNUNET_JSON_PackSpec ps = { | ||
173 | .field_name = name, | ||
174 | .object = o | ||
175 | }; | ||
176 | |||
177 | if (NULL == o) | ||
178 | return ps; | ||
179 | (void) json_incref (o); | ||
180 | if (! json_is_object (o)) | ||
181 | { | ||
182 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
183 | "Expected JSON object for field `%s'\n", | ||
184 | name); | ||
185 | GNUNET_assert (0); | ||
186 | } | ||
187 | return ps; | ||
188 | } | ||
189 | |||
190 | |||
191 | struct GNUNET_JSON_PackSpec | ||
192 | GNUNET_JSON_pack_array_steal (const char *name, | ||
193 | json_t *a) | ||
194 | { | ||
195 | struct GNUNET_JSON_PackSpec ps = { | ||
196 | .field_name = name, | ||
197 | .object = a | ||
198 | }; | ||
199 | |||
200 | if (NULL == a) | ||
201 | return ps; | ||
202 | if (! json_is_array (a)) | ||
203 | { | ||
204 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
205 | "Expected JSON array for field `%s'\n", | ||
206 | name); | ||
207 | GNUNET_assert (0); | ||
208 | } | ||
209 | return ps; | ||
210 | } | ||
211 | |||
212 | |||
213 | struct GNUNET_JSON_PackSpec | ||
214 | GNUNET_JSON_pack_array_incref (const char *name, | ||
215 | json_t *a) | ||
216 | { | ||
217 | struct GNUNET_JSON_PackSpec ps = { | ||
218 | .field_name = name, | ||
219 | .object = a | ||
220 | }; | ||
221 | |||
222 | if (NULL == a) | ||
223 | return ps; | ||
224 | (void) json_incref (a); | ||
225 | if (! json_is_array (a)) | ||
226 | { | ||
227 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
228 | "Expected JSON array for field `%s'\n", | ||
229 | name); | ||
230 | GNUNET_assert (0); | ||
231 | } | ||
232 | return ps; | ||
233 | } | ||
234 | |||
235 | |||
236 | struct GNUNET_JSON_PackSpec | ||
237 | GNUNET_JSON_pack_data_varsize (const char *name, | ||
238 | const void *blob, | ||
239 | size_t blob_size) | ||
240 | { | ||
241 | struct GNUNET_JSON_PackSpec ps = { | ||
242 | .field_name = name, | ||
243 | .object = (NULL != blob) | ||
244 | ? GNUNET_JSON_from_data (blob, | ||
245 | blob_size) | ||
246 | : NULL | ||
247 | }; | ||
248 | |||
249 | return ps; | ||
250 | } | ||
251 | |||
252 | |||
253 | struct GNUNET_JSON_PackSpec | ||
254 | GNUNET_JSON_pack_timestamp (const char *name, | ||
255 | struct GNUNET_TIME_Timestamp t) | ||
256 | { | ||
257 | struct GNUNET_JSON_PackSpec ps = { | ||
258 | .field_name = name | ||
259 | }; | ||
260 | |||
261 | if (! GNUNET_TIME_absolute_is_zero (t.abs_time)) | ||
262 | { | ||
263 | ps.object = GNUNET_JSON_from_timestamp (t); | ||
264 | GNUNET_assert (NULL != ps.object); | ||
265 | } | ||
266 | else | ||
267 | { | ||
268 | ps.object = NULL; | ||
269 | } | ||
270 | return ps; | ||
271 | } | ||
272 | |||
273 | |||
274 | struct GNUNET_JSON_PackSpec | ||
275 | GNUNET_JSON_pack_timestamp_nbo (const char *name, | ||
276 | struct GNUNET_TIME_TimestampNBO at) | ||
277 | { | ||
278 | return GNUNET_JSON_pack_timestamp (name, | ||
279 | GNUNET_TIME_timestamp_ntoh (at)); | ||
280 | } | ||
281 | |||
282 | |||
283 | struct GNUNET_JSON_PackSpec | ||
284 | GNUNET_JSON_pack_time_rel (const char *name, | ||
285 | struct GNUNET_TIME_Relative rt) | ||
286 | { | ||
287 | json_t *json; | ||
288 | |||
289 | json = GNUNET_JSON_from_time_rel (rt); | ||
290 | GNUNET_assert (NULL != json); | ||
291 | return GNUNET_JSON_pack_object_steal (name, | ||
292 | json); | ||
293 | } | ||
294 | |||
295 | |||
296 | struct GNUNET_JSON_PackSpec | ||
297 | GNUNET_JSON_pack_time_rel_nbo (const char *name, | ||
298 | struct GNUNET_TIME_RelativeNBO rt) | ||
299 | { | ||
300 | return GNUNET_JSON_pack_time_rel (name, | ||
301 | GNUNET_TIME_relative_ntoh (rt)); | ||
302 | } | ||
303 | |||
304 | |||
305 | struct GNUNET_JSON_PackSpec | ||
306 | GNUNET_JSON_pack_rsa_public_key (const char *name, | ||
307 | const struct GNUNET_CRYPTO_RsaPublicKey *pk) | ||
308 | { | ||
309 | struct GNUNET_JSON_PackSpec ps = { | ||
310 | .field_name = name, | ||
311 | .object = GNUNET_JSON_from_rsa_public_key (pk) | ||
312 | }; | ||
313 | |||
314 | return ps; | ||
315 | } | ||
316 | |||
317 | |||
318 | struct GNUNET_JSON_PackSpec | ||
319 | GNUNET_JSON_pack_rsa_signature (const char *name, | ||
320 | const struct GNUNET_CRYPTO_RsaSignature *sig) | ||
321 | { | ||
322 | struct GNUNET_JSON_PackSpec ps = { | ||
323 | .field_name = name, | ||
324 | .object = GNUNET_JSON_from_rsa_signature (sig) | ||
325 | }; | ||
326 | |||
327 | return ps; | ||
328 | } | ||
329 | |||
330 | |||
331 | /* end of json_pack.c */ | ||