diff options
Diffstat (limited to 'src/include/gnunet_json_lib.h')
-rw-r--r-- | src/include/gnunet_json_lib.h | 180 |
1 files changed, 175 insertions, 5 deletions
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h index 2002a0130..44dbf8965 100644 --- a/src/include/gnunet_json_lib.h +++ b/src/include/gnunet_json_lib.h | |||
@@ -27,6 +27,7 @@ | |||
27 | #ifndef GNUNET_JSON_LIB_H | 27 | #ifndef GNUNET_JSON_LIB_H |
28 | #define GNUNET_JSON_LIB_H | 28 | #define GNUNET_JSON_LIB_H |
29 | 29 | ||
30 | |||
30 | #include "gnunet_util_lib.h" | 31 | #include "gnunet_util_lib.h" |
31 | #include <jansson.h> | 32 | #include <jansson.h> |
32 | #include <microhttpd.h> | 33 | #include <microhttpd.h> |
@@ -129,7 +130,7 @@ struct GNUNET_JSON_Specification | |||
129 | * @param root the JSON node to start the navigation at. | 130 | * @param root the JSON node to start the navigation at. |
130 | * @param spec parse specification array | 131 | * @param spec parse specification array |
131 | * @param[out] error_json_name which JSON field was problematic | 132 | * @param[out] error_json_name which JSON field was problematic |
132 | * @param[out] which index into @a spec did we encounter an error | 133 | * @param[out] error_line which index into @a spec did we encounter an error |
133 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 134 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
134 | */ | 135 | */ |
135 | enum GNUNET_GenericReturnValue | 136 | enum GNUNET_GenericReturnValue |
@@ -141,7 +142,13 @@ GNUNET_JSON_parse (const json_t *root, | |||
141 | 142 | ||
142 | /** | 143 | /** |
143 | * Frees all elements allocated during a #GNUNET_JSON_parse() | 144 | * Frees all elements allocated during a #GNUNET_JSON_parse() |
144 | * operation. | 145 | * operation. Convenience function to be called if cleaning |
146 | * up all heap-allocated data from a #GNUNET_JSON_parse() is | ||
147 | * desired. The function does not have to be called if no data | ||
148 | * was heap-allocated (e.g. only integers, strings and fixed-sized | ||
149 | * data was used), or if the application calls the respective | ||
150 | * code to free the heap (not always #GNUNET_free(), depends | ||
151 | * on the data type!) on the returned heap-allocated data itself. | ||
145 | * | 152 | * |
146 | * @param spec specification of the parse operation | 153 | * @param spec specification of the parse operation |
147 | */ | 154 | */ |
@@ -197,6 +204,29 @@ GNUNET_JSON_spec_fixed (const char *name, | |||
197 | 204 | ||
198 | 205 | ||
199 | /** | 206 | /** |
207 | * Variable size object (in network byte order, encoded using base64 encoding). | ||
208 | * | ||
209 | * @param name name of the JSON field | ||
210 | * @param[out] obj pointer where to write the data, must have @a size bytes | ||
211 | * @param size number of bytes expected in @a obj | ||
212 | */ | ||
213 | struct GNUNET_JSON_Specification | ||
214 | GNUNET_JSON_spec_fixed64 (const char *name, | ||
215 | void *obj, | ||
216 | size_t size); | ||
217 | |||
218 | |||
219 | /** | ||
220 | * Fixed size object (in network byte order, encoded using base64 encoding). | ||
221 | * | ||
222 | * @param name name of the JSON field | ||
223 | * @param obj pointer where to write the data (type of `*obj` will determine size) | ||
224 | */ | ||
225 | #define GNUNET_JSON_spec_fixed64_auto(name, obj) \ | ||
226 | GNUNET_JSON_spec_fixed (name, obj, sizeof(*obj)) | ||
227 | |||
228 | |||
229 | /** | ||
200 | * Variable size object (in network byte order, encoded using | 230 | * Variable size object (in network byte order, encoded using |
201 | * Crockford Base32hex encoding). | 231 | * Crockford Base32hex encoding). |
202 | * | 232 | * |
@@ -222,7 +252,8 @@ GNUNET_JSON_spec_string (const char *name, | |||
222 | 252 | ||
223 | 253 | ||
224 | /** | 254 | /** |
225 | * JSON object. | 255 | * JSON object or array. Reference counter is |
256 | * incremented. | ||
226 | * | 257 | * |
227 | * @param name name of the JSON field | 258 | * @param name name of the JSON field |
228 | * @param[out] jsonp where to store the JSON found under @a name | 259 | * @param[out] jsonp where to store the JSON found under @a name |
@@ -233,6 +264,28 @@ GNUNET_JSON_spec_json (const char *name, | |||
233 | 264 | ||
234 | 265 | ||
235 | /** | 266 | /** |
267 | * JSON object, reference counter not incremented. | ||
268 | * | ||
269 | * @param name name of the JSON field | ||
270 | * @param[out] jsonp where to store the JSON found under @a name | ||
271 | */ | ||
272 | struct GNUNET_JSON_Specification | ||
273 | GNUNET_JSON_spec_object_const (const char *name, | ||
274 | const json_t **jsonp); | ||
275 | |||
276 | |||
277 | /** | ||
278 | * JSON array, reference counter not incremented. | ||
279 | * | ||
280 | * @param name name of the JSON field | ||
281 | * @param[out] jsonp where to store the JSON found under @a name | ||
282 | */ | ||
283 | struct GNUNET_JSON_Specification | ||
284 | GNUNET_JSON_spec_array_const (const char *name, | ||
285 | const json_t **jsonp); | ||
286 | |||
287 | |||
288 | /** | ||
236 | * boolean. | 289 | * boolean. |
237 | * | 290 | * |
238 | * @param name name of the JSON field | 291 | * @param name name of the JSON field |
@@ -244,6 +297,17 @@ GNUNET_JSON_spec_bool (const char *name, | |||
244 | 297 | ||
245 | 298 | ||
246 | /** | 299 | /** |
300 | * double. | ||
301 | * | ||
302 | * @param name name of the JSON field | ||
303 | * @param[out] f where to store the double found under @a name | ||
304 | */ | ||
305 | struct GNUNET_JSON_Specification | ||
306 | GNUNET_JSON_spec_double (const char *name, | ||
307 | double *f); | ||
308 | |||
309 | |||
310 | /** | ||
247 | * 8-bit integer. | 311 | * 8-bit integer. |
248 | * | 312 | * |
249 | * @param name name of the JSON field | 313 | * @param name name of the JSON field |
@@ -315,7 +379,7 @@ GNUNET_JSON_spec_boolean (const char *name, | |||
315 | * Timestamp. | 379 | * Timestamp. |
316 | * | 380 | * |
317 | * @param name name of the JSON field | 381 | * @param name name of the JSON field |
318 | * @param[out] at where to store the absolute time found under @a name | 382 | * @param[out] t at where to store the absolute time found under @a name |
319 | */ | 383 | */ |
320 | struct GNUNET_JSON_Specification | 384 | struct GNUNET_JSON_Specification |
321 | GNUNET_JSON_spec_timestamp (const char *name, | 385 | GNUNET_JSON_spec_timestamp (const char *name, |
@@ -366,6 +430,17 @@ GNUNET_JSON_spec_rsa_signature (const char *name, | |||
366 | struct GNUNET_CRYPTO_RsaSignature **sig); | 430 | struct GNUNET_CRYPTO_RsaSignature **sig); |
367 | 431 | ||
368 | 432 | ||
433 | /** | ||
434 | * Specification for parsing a blinded message. | ||
435 | * | ||
436 | * @param name name of the JSON field | ||
437 | * @param sig where to store the blinded message found under @a name | ||
438 | */ | ||
439 | struct GNUNET_JSON_Specification | ||
440 | GNUNET_JSON_spec_blinded_message (const char *name, | ||
441 | struct GNUNET_CRYPTO_BlindedMessage **msg); | ||
442 | |||
443 | |||
369 | /* ****************** Generic generator interface ******************* */ | 444 | /* ****************** Generic generator interface ******************* */ |
370 | 445 | ||
371 | 446 | ||
@@ -378,7 +453,21 @@ GNUNET_JSON_spec_rsa_signature (const char *name, | |||
378 | * @return json string that encodes @a data | 453 | * @return json string that encodes @a data |
379 | */ | 454 | */ |
380 | json_t * | 455 | json_t * |
381 | GNUNET_JSON_from_data (const void *data, size_t size); | 456 | GNUNET_JSON_from_data (const void *data, |
457 | size_t size); | ||
458 | |||
459 | |||
460 | /** | ||
461 | * Convert binary data to a JSON string with base64 | ||
462 | * encoding. | ||
463 | * | ||
464 | * @param data binary data | ||
465 | * @param size size of @a data in bytes | ||
466 | * @return json string that encodes @a data | ||
467 | */ | ||
468 | json_t * | ||
469 | GNUNET_JSON_from_data64 (const void *data, | ||
470 | size_t size); | ||
382 | 471 | ||
383 | 472 | ||
384 | /** | 473 | /** |
@@ -393,6 +482,17 @@ GNUNET_JSON_from_data (const void *data, size_t size); | |||
393 | 482 | ||
394 | 483 | ||
395 | /** | 484 | /** |
485 | * Convert binary data to a JSON string with base64 | ||
486 | * encoding. | ||
487 | * | ||
488 | * @param ptr binary data, sizeof (*ptr) must yield correct size | ||
489 | * @return json string that encodes @a data | ||
490 | */ | ||
491 | #define GNUNET_JSON_from_data64_auto(ptr) \ | ||
492 | GNUNET_JSON_from_data64 (ptr, sizeof(*ptr)) | ||
493 | |||
494 | |||
495 | /** | ||
396 | * Convert timestamp to a json string. | 496 | * Convert timestamp to a json string. |
397 | * | 497 | * |
398 | * @param stamp the time stamp | 498 | * @param stamp the time stamp |
@@ -632,6 +732,19 @@ GNUNET_JSON_pack_bool (const char *name, | |||
632 | 732 | ||
633 | /** | 733 | /** |
634 | * Generate packer instruction for a JSON field of type | 734 | * Generate packer instruction for a JSON field of type |
735 | * double. | ||
736 | * | ||
737 | * @param name name of the field to add to the object | ||
738 | * @param f double value | ||
739 | * @return json pack specification | ||
740 | */ | ||
741 | struct GNUNET_JSON_PackSpec | ||
742 | GNUNET_JSON_pack_double (const char *name, | ||
743 | double f); | ||
744 | |||
745 | |||
746 | /** | ||
747 | * Generate packer instruction for a JSON field of type | ||
635 | * string. | 748 | * string. |
636 | * | 749 | * |
637 | * @param name name of the field to add to the object | 750 | * @param name name of the field to add to the object |
@@ -755,6 +868,37 @@ GNUNET_JSON_pack_data_varsize (const char *name, | |||
755 | 868 | ||
756 | /** | 869 | /** |
757 | * Generate packer instruction for a JSON field of type | 870 | * Generate packer instruction for a JSON field of type |
871 | * variable size binary blob. | ||
872 | * Use base64-encoding, instead of the more common | ||
873 | * Crockford base32-encoding. | ||
874 | * | ||
875 | * @param name name of the field to add to the object | ||
876 | * @param blob binary data to pack | ||
877 | * @param blob_size number of bytes in @a blob | ||
878 | * @return json pack specification | ||
879 | */ | ||
880 | struct GNUNET_JSON_PackSpec | ||
881 | GNUNET_JSON_pack_data64_varsize (const char *name, | ||
882 | const void *blob, | ||
883 | size_t blob_size); | ||
884 | |||
885 | |||
886 | /** | ||
887 | * Generate packer instruction for a JSON field where the | ||
888 | * size is automatically determined from the argument. | ||
889 | * Use base64-encoding, instead of the more common | ||
890 | * Crockford base32-encoding. | ||
891 | * | ||
892 | * @param name name of the field to add to the object | ||
893 | * @param blob data to pack, must not be an array | ||
894 | * @return json pack specification | ||
895 | */ | ||
896 | #define GNUNET_JSON_pack_data64_auto(name,blob) \ | ||
897 | GNUNET_JSON_pack_data64_varsize (name, blob, sizeof (*blob)) | ||
898 | |||
899 | |||
900 | /** | ||
901 | * Generate packer instruction for a JSON field of type | ||
758 | * timestamp. | 902 | * timestamp. |
759 | * | 903 | * |
760 | * @param name name of the field to add to the object | 904 | * @param name name of the field to add to the object |
@@ -833,6 +977,32 @@ GNUNET_JSON_pack_rsa_signature (const char *name, | |||
833 | const struct GNUNET_CRYPTO_RsaSignature *sig); | 977 | const struct GNUNET_CRYPTO_RsaSignature *sig); |
834 | 978 | ||
835 | 979 | ||
980 | /** | ||
981 | * Generate packer instruction for a JSON field of type | ||
982 | * unblinded signature. | ||
983 | * | ||
984 | * @param name name of the field to add to the object | ||
985 | * @param sig unblinded signature | ||
986 | * @return json pack specification | ||
987 | */ | ||
988 | struct GNUNET_JSON_PackSpec | ||
989 | GNUNET_JSON_pack_unblinded_signature (const char *name, | ||
990 | const struct GNUNET_CRYPTO_UnblindedSignature *sig); | ||
991 | |||
992 | |||
993 | /** | ||
994 | * Generate packer instruction for a JSON field of type | ||
995 | * blinded message. | ||
996 | * | ||
997 | * @param name name of the field to add to the object | ||
998 | * @param msg blinded message | ||
999 | * @return json pack specification | ||
1000 | */ | ||
1001 | struct GNUNET_JSON_PackSpec | ||
1002 | GNUNET_JSON_pack_blinded_message (const char *name, | ||
1003 | const struct GNUNET_CRYPTO_BlindedMessage *msg); | ||
1004 | |||
1005 | |||
836 | #endif | 1006 | #endif |
837 | 1007 | ||
838 | /* end of gnunet_json_lib.h */ | 1008 | /* end of gnunet_json_lib.h */ |