diff options
m--------- | contrib/gana | 0 | ||||
-rw-r--r-- | src/include/gnunet_json_lib.h | 81 | ||||
-rw-r--r-- | src/json/json_generator.c | 30 | ||||
-rw-r--r-- | src/json/json_helper.c | 65 | ||||
-rw-r--r-- | src/json/json_pack.c | 17 |
5 files changed, 191 insertions, 2 deletions
diff --git a/contrib/gana b/contrib/gana | |||
Subproject 7bfe1654eeab7e7eacb4f6eb45ad52ffe4511c4 | Subproject 1ed41552f6013e5f48c68389d60e3d5d61892a0 | ||
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h index 2002a0130..af63c5e42 100644 --- a/src/include/gnunet_json_lib.h +++ b/src/include/gnunet_json_lib.h | |||
@@ -197,6 +197,29 @@ GNUNET_JSON_spec_fixed (const char *name, | |||
197 | 197 | ||
198 | 198 | ||
199 | /** | 199 | /** |
200 | * Variable size object (in network byte order, encoded using base64 encoding). | ||
201 | * | ||
202 | * @param name name of the JSON field | ||
203 | * @param[out] obj pointer where to write the data, must have @a size bytes | ||
204 | * @param size number of bytes expected in @a obj | ||
205 | */ | ||
206 | struct GNUNET_JSON_Specification | ||
207 | GNUNET_JSON_spec_fixed64 (const char *name, | ||
208 | void *obj, | ||
209 | size_t size); | ||
210 | |||
211 | |||
212 | /** | ||
213 | * Fixed size object (in network byte order, encoded using base64 encoding). | ||
214 | * | ||
215 | * @param name name of the JSON field | ||
216 | * @param obj pointer where to write the data (type of `*obj` will determine size) | ||
217 | */ | ||
218 | #define GNUNET_JSON_spec_fixed64_auto(name, obj) \ | ||
219 | GNUNET_JSON_spec_fixed (name, obj, sizeof(*obj)) | ||
220 | |||
221 | |||
222 | /** | ||
200 | * Variable size object (in network byte order, encoded using | 223 | * Variable size object (in network byte order, encoded using |
201 | * Crockford Base32hex encoding). | 224 | * Crockford Base32hex encoding). |
202 | * | 225 | * |
@@ -378,7 +401,21 @@ GNUNET_JSON_spec_rsa_signature (const char *name, | |||
378 | * @return json string that encodes @a data | 401 | * @return json string that encodes @a data |
379 | */ | 402 | */ |
380 | json_t * | 403 | json_t * |
381 | GNUNET_JSON_from_data (const void *data, size_t size); | 404 | GNUNET_JSON_from_data (const void *data, |
405 | size_t size); | ||
406 | |||
407 | |||
408 | /** | ||
409 | * Convert binary data to a JSON string with base64 | ||
410 | * encoding. | ||
411 | * | ||
412 | * @param data binary data | ||
413 | * @param size size of @a data in bytes | ||
414 | * @return json string that encodes @a data | ||
415 | */ | ||
416 | json_t * | ||
417 | GNUNET_JSON_from_data64 (const void *data, | ||
418 | size_t size); | ||
382 | 419 | ||
383 | 420 | ||
384 | /** | 421 | /** |
@@ -393,6 +430,17 @@ GNUNET_JSON_from_data (const void *data, size_t size); | |||
393 | 430 | ||
394 | 431 | ||
395 | /** | 432 | /** |
433 | * Convert binary data to a JSON string with base64 | ||
434 | * encoding. | ||
435 | * | ||
436 | * @param ptr binary data, sizeof (*ptr) must yield correct size | ||
437 | * @return json string that encodes @a data | ||
438 | */ | ||
439 | #define GNUNET_JSON_from_data64_auto(ptr) \ | ||
440 | GNUNET_JSON_from_data64 (ptr, sizeof(*ptr)) | ||
441 | |||
442 | |||
443 | /** | ||
396 | * Convert timestamp to a json string. | 444 | * Convert timestamp to a json string. |
397 | * | 445 | * |
398 | * @param stamp the time stamp | 446 | * @param stamp the time stamp |
@@ -755,6 +803,37 @@ GNUNET_JSON_pack_data_varsize (const char *name, | |||
755 | 803 | ||
756 | /** | 804 | /** |
757 | * Generate packer instruction for a JSON field of type | 805 | * Generate packer instruction for a JSON field of type |
806 | * variable size binary blob. | ||
807 | * Use base64-encoding, instead of the more common | ||
808 | * Crockford base32-encoding. | ||
809 | * | ||
810 | * @param name name of the field to add to the object | ||
811 | * @param blob binary data to pack | ||
812 | * @param blob_size number of bytes in @a blob | ||
813 | * @return json pack specification | ||
814 | */ | ||
815 | struct GNUNET_JSON_PackSpec | ||
816 | GNUNET_JSON_pack_data64_varsize (const char *name, | ||
817 | const void *blob, | ||
818 | size_t blob_size); | ||
819 | |||
820 | |||
821 | /** | ||
822 | * Generate packer instruction for a JSON field where the | ||
823 | * size is automatically determined from the argument. | ||
824 | * Use base64-encoding, instead of the more common | ||
825 | * Crockford base32-encoding. | ||
826 | * | ||
827 | * @param name name of the field to add to the object | ||
828 | * @param blob data to pack, must not be an array | ||
829 | * @return json pack specification | ||
830 | */ | ||
831 | #define GNUNET_JSON_pack_data64_auto(name,blob) \ | ||
832 | GNUNET_JSON_pack_data64_varsize (name, blob, sizeof (*blob)) | ||
833 | |||
834 | |||
835 | /** | ||
836 | * Generate packer instruction for a JSON field of type | ||
758 | * timestamp. | 837 | * timestamp. |
759 | * | 838 | * |
760 | * @param name name of the field to add to the object | 839 | * @param name name of the field to add to the object |
diff --git a/src/json/json_generator.c b/src/json/json_generator.c index eb275712c..4fda86e32 100644 --- a/src/json/json_generator.c +++ b/src/json/json_generator.c | |||
@@ -50,6 +50,36 @@ GNUNET_JSON_from_data (const void *data, | |||
50 | 50 | ||
51 | 51 | ||
52 | json_t * | 52 | json_t * |
53 | GNUNET_JSON_from_data64 (const void *data, | ||
54 | size_t size) | ||
55 | { | ||
56 | char *buf = NULL; | ||
57 | json_t *json; | ||
58 | size_t len; | ||
59 | |||
60 | if ((size * 8 + 5) / 6 + 1 >= | ||
61 | GNUNET_MAX_MALLOC_CHECKED) | ||
62 | { | ||
63 | GNUNET_break (0); | ||
64 | return NULL; | ||
65 | } | ||
66 | len = GNUNET_STRINGS_base64_encode (data, | ||
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 | } | ||
80 | |||
81 | |||
82 | json_t * | ||
53 | GNUNET_JSON_from_timestamp (struct GNUNET_TIME_Timestamp stamp) | 83 | GNUNET_JSON_from_timestamp (struct GNUNET_TIME_Timestamp stamp) |
54 | { | 84 | { |
55 | json_t *j; | 85 | json_t *j; |
diff --git a/src/json/json_helper.c b/src/json/json_helper.c index a15dc74c0..39949819f 100644 --- a/src/json/json_helper.c +++ b/src/json/json_helper.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet | 2 | This file is part of GNUnet |
3 | Copyright (C) 2014, 2015, 2016 GNUnet e.V. | 3 | Copyright (C) 2014-2022 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 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 | 6 | under the terms of the GNU Affero General Public License as published |
@@ -104,6 +104,69 @@ GNUNET_JSON_spec_fixed (const char *name, | |||
104 | 104 | ||
105 | 105 | ||
106 | /** | 106 | /** |
107 | * Parse given JSON object to fixed size data | ||
108 | * | ||
109 | * @param cls closure, NULL | ||
110 | * @param root the json object representing data | ||
111 | * @param[out] spec where to write the data | ||
112 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
113 | */ | ||
114 | static enum GNUNET_GenericReturnValue | ||
115 | parse_fixed64_data (void *cls, | ||
116 | json_t *root, | ||
117 | struct GNUNET_JSON_Specification *spec) | ||
118 | { | ||
119 | const char *enc; | ||
120 | unsigned int len; | ||
121 | void *output; | ||
122 | size_t olen; | ||
123 | |||
124 | if (NULL == (enc = json_string_value (root))) | ||
125 | { | ||
126 | GNUNET_break_op (0); | ||
127 | return GNUNET_SYSERR; | ||
128 | } | ||
129 | len = strlen (enc); | ||
130 | output = NULL; | ||
131 | olen = GNUNET_STRINGS_base64_decode (enc, | ||
132 | len, | ||
133 | &output); | ||
134 | if (olen != spec->ptr_size) | ||
135 | { | ||
136 | GNUNET_break_op (0); | ||
137 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
138 | "Field `%s' has wrong length\n", | ||
139 | spec->field); | ||
140 | return GNUNET_SYSERR; | ||
141 | } | ||
142 | memcpy (spec->ptr, | ||
143 | output, | ||
144 | olen); | ||
145 | GNUNET_free (output); | ||
146 | return GNUNET_OK; | ||
147 | } | ||
148 | |||
149 | |||
150 | struct GNUNET_JSON_Specification | ||
151 | GNUNET_JSON_spec_fixed64 (const char *name, | ||
152 | void *obj, | ||
153 | size_t size) | ||
154 | { | ||
155 | struct GNUNET_JSON_Specification ret = { | ||
156 | .parser = &parse_fixed64_data, | ||
157 | .cleaner = NULL, | ||
158 | .cls = NULL, | ||
159 | .field = name, | ||
160 | .ptr = obj, | ||
161 | .ptr_size = size, | ||
162 | .size_ptr = NULL | ||
163 | }; | ||
164 | |||
165 | return ret; | ||
166 | } | ||
167 | |||
168 | |||
169 | /** | ||
107 | * Parse given JSON object to variable size data | 170 | * Parse given JSON object to variable size data |
108 | * | 171 | * |
109 | * @param cls closure, NULL | 172 | * @param cls closure, NULL |
diff --git a/src/json/json_pack.c b/src/json/json_pack.c index cc1ca3e97..8fc806086 100644 --- a/src/json/json_pack.c +++ b/src/json/json_pack.c | |||
@@ -251,6 +251,23 @@ GNUNET_JSON_pack_data_varsize (const char *name, | |||
251 | 251 | ||
252 | 252 | ||
253 | struct GNUNET_JSON_PackSpec | 253 | struct GNUNET_JSON_PackSpec |
254 | GNUNET_JSON_pack_data64_varsize (const char *name, | ||
255 | const void *blob, | ||
256 | size_t blob_size) | ||
257 | { | ||
258 | struct GNUNET_JSON_PackSpec ps = { | ||
259 | .field_name = name, | ||
260 | .object = (NULL != blob) | ||
261 | ? GNUNET_JSON_from_data64 (blob, | ||
262 | blob_size) | ||
263 | : NULL | ||
264 | }; | ||
265 | |||
266 | return ps; | ||
267 | } | ||
268 | |||
269 | |||
270 | struct GNUNET_JSON_PackSpec | ||
254 | GNUNET_JSON_pack_timestamp (const char *name, | 271 | GNUNET_JSON_pack_timestamp (const char *name, |
255 | struct GNUNET_TIME_Timestamp t) | 272 | struct GNUNET_TIME_Timestamp t) |
256 | { | 273 | { |