gnunet-android

GNUnet for Android
Log | Files | Refs | README

gnunet_json_lib.h (28021B)


      1 /*
      2    This file is part of GNUnet
      3    Copyright (C) 2014, 2015, 2016, 2020, 2024 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 gnunet_json_lib.h
     22  * @brief functions to parse JSON objects into GNUnet objects
     23  * @author Florian Dold
     24  * @author Benedikt Mueller
     25  * @author Christian Grothoff
     26  */
     27 #ifndef GNUNET_JSON_LIB_H
     28 #define GNUNET_JSON_LIB_H
     29 
     30 #include "gnunet_util_lib.h"
     31 #include <jansson.h>
     32 
     33 /* ****************** Generic parser interface ******************* */
     34 
     35 /**
     36  * @brief Entry in parser specification for #GNUNET_JSON_parse().
     37  */
     38 struct GNUNET_JSON_Specification;
     39 
     40 
     41 /**
     42  * Function called to parse JSON argument.
     43  *
     44  * @param cls closure
     45  * @param root JSON to parse
     46  * @param spec our specification entry with further details
     47  * @return #GNUNET_SYSERR on error,
     48  *         #GNUNET_OK on success
     49  */
     50 typedef enum GNUNET_GenericReturnValue
     51 (*GNUNET_JSON_Parser)(void *cls,
     52                       json_t *root,
     53                       struct GNUNET_JSON_Specification *spec);
     54 
     55 
     56 /**
     57  * Function called to clean up data from earlier parsing.
     58  *
     59  * @param cls closure
     60  * @param spec our specification entry with data to clean.
     61  */
     62 typedef void
     63 (*GNUNET_JSON_Cleaner) (void *cls,
     64                         struct GNUNET_JSON_Specification *spec);
     65 
     66 
     67 /**
     68  * @brief Entry in parser specification for #GNUNET_JSON_parse().
     69  */
     70 struct GNUNET_JSON_Specification
     71 {
     72   /**
     73    * Function for how to parse this type of entry.
     74    */
     75   GNUNET_JSON_Parser parser;
     76 
     77   /**
     78    * Function for how to clean up this type of entry.
     79    */
     80   GNUNET_JSON_Cleaner cleaner;
     81 
     82   /**
     83    * Closure for @e parser and @e cleaner.
     84    */
     85   void *cls;
     86 
     87   /**
     88    * Name of the field to parse, use NULL to get the JSON
     89    * of the main object instead of the JSON of an individual field.
     90    */
     91   const char *field;
     92 
     93   /**
     94    * Pointer, details specific to the @e parser.
     95    */
     96   void *ptr;
     97 
     98   /**
     99    * Pointer to set to true if this argument is
    100    * indeed missing. Can be NULL.
    101    */
    102   bool *missing;
    103 
    104   /**
    105    * Where should we store the final size of @e ptr.
    106    */
    107   size_t *size_ptr;
    108 
    109   /**
    110    * Number of bytes available in @e ptr.
    111    */
    112   size_t ptr_size;
    113 
    114   /**
    115    * Set to true if this component is optional.
    116    */
    117   bool is_optional;
    118 };
    119 
    120 
    121 /**
    122  * Navigate and parse data in a JSON tree.  Tries to parse the @a root
    123  * to find all of the values given in the @a spec.  If one of the
    124  * entries in @a spec cannot be found or parsed, the name of the JSON
    125  * field is returned in @a error_json_name, and the offset of the
    126  * entry in @a spec is returned in @a error_line.
    127  *
    128  * @param root the JSON node to start the navigation at.
    129  * @param[in,out] spec parse specification array
    130  * @param[out] error_json_name which JSON field was problematic
    131  * @param[out] error_line which index into @a spec did we encounter an error
    132  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
    133  */
    134 enum GNUNET_GenericReturnValue
    135 GNUNET_JSON_parse (const json_t *root,
    136                    struct GNUNET_JSON_Specification *spec,
    137                    const char **error_json_name,
    138                    unsigned int *error_line);
    139 
    140 
    141 /**
    142  * Frees all elements allocated during a #GNUNET_JSON_parse()
    143  * operation.  Convenience function to be called if cleaning
    144  * up all heap-allocated data from a #GNUNET_JSON_parse() is
    145  * desired. The function does not have to be called if no data
    146  * was heap-allocated (e.g. only integers, strings and fixed-sized
    147  * data was used), or if the application calls the respective
    148  * code to free the heap (not always #GNUNET_free(), depends
    149  * on the data type!) on the returned heap-allocated data itself.
    150  *
    151  * @param[in,out] spec specification of the parse operation
    152  */
    153 void
    154 GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec);
    155 
    156 
    157 /* ****************** Canonical parser specifications ******************* */
    158 
    159 
    160 /**
    161  * End of a parser specification.
    162  */
    163 struct GNUNET_JSON_Specification
    164 GNUNET_JSON_spec_end (void);
    165 
    166 
    167 /**
    168  * Set the "optional" flag for a parser specification entry.
    169  *
    170  * @param spec specification to modify
    171  * @param[out] missing set to true if the argument is missing, NULL is allowed.
    172  * @return spec copy of @a spec with optional bit set
    173  */
    174 struct GNUNET_JSON_Specification
    175 GNUNET_JSON_spec_mark_optional (struct GNUNET_JSON_Specification spec,
    176                                 bool *missing);
    177 
    178 
    179 /**
    180  * Variable size object (in network byte order, encoded using Crockford
    181  * Base32hex encoding).
    182  *
    183  * @param name name of the JSON field
    184  * @param[out] obj pointer where to write the data, must have @a size bytes
    185  * @param size number of bytes expected in @a obj
    186  */
    187 struct GNUNET_JSON_Specification
    188 GNUNET_JSON_spec_fixed (const char *name,
    189                         void *obj,
    190                         size_t size);
    191 
    192 
    193 /**
    194  * Fixed size object (in network byte order, encoded using Crockford
    195  * Base32hex encoding).
    196  *
    197  * @param name name of the JSON field
    198  * @param obj pointer where to write the data (type of `*obj` will determine size)
    199  */
    200 #define GNUNET_JSON_spec_fixed_auto(name, obj) \
    201         GNUNET_JSON_spec_fixed (name, (obj), sizeof(*(obj)))
    202 
    203 
    204 /**
    205  * Variable size object (in network byte order, encoded using base64 encoding).
    206  *
    207  * @param name name of the JSON field
    208  * @param[out] obj pointer where to write the data, must have @a size bytes
    209  * @param size number of bytes expected in @a obj
    210  */
    211 struct GNUNET_JSON_Specification
    212 GNUNET_JSON_spec_fixed64 (const char *name,
    213                           void *obj,
    214                           size_t size);
    215 
    216 
    217 /**
    218  * Fixed size object (in network byte order, encoded using base64 encoding).
    219  *
    220  * @param name name of the JSON field
    221  * @param obj pointer where to write the data (type of `*obj` will determine size)
    222  */
    223 #define GNUNET_JSON_spec_fixed64_auto(name, obj) \
    224         GNUNET_JSON_spec_fixed (name, (obj), sizeof(*(obj)))
    225 
    226 
    227 /**
    228  * Variable size object (in network byte order, encoded using
    229  * Crockford Base32hex encoding).
    230  *
    231  * @param name name of the JSON field
    232  * @param[out] obj pointer where to write the data, will be allocated
    233  * @param[out] size where to store the number of bytes allocated for @a obj
    234  */
    235 struct GNUNET_JSON_Specification
    236 GNUNET_JSON_spec_varsize (const char *name,
    237                           void **obj,
    238                           size_t *size);
    239 
    240 
    241 /**
    242  * The expected field stores a string.
    243  *
    244  * @param name name of the JSON field
    245  * @param strptr where to store a pointer to the field
    246  */
    247 struct GNUNET_JSON_Specification
    248 GNUNET_JSON_spec_string (const char *name,
    249                          const char **strptr);
    250 
    251 
    252 /**
    253  * The expected field stores a string.
    254  *
    255  * @param name name of the JSON field
    256  * @param strptr where to store a pointer to the field
    257  */
    258 struct GNUNET_JSON_Specification
    259 GNUNET_JSON_spec_string_copy (const char *name,
    260                               char **strptr);
    261 
    262 
    263 /**
    264  * JSON object or array. Reference counter is
    265  * incremented.
    266  *
    267  * @param name name of the JSON field
    268  * @param[out] jsonp where to store the JSON found under @a name
    269  */
    270 struct GNUNET_JSON_Specification
    271 GNUNET_JSON_spec_json (const char *name,
    272                        json_t **jsonp);
    273 
    274 
    275 /**
    276  * JSON object, reference counter not incremented.
    277  *
    278  * @param name name of the JSON field
    279  * @param[out] jsonp where to store the JSON found under @a name
    280  */
    281 struct GNUNET_JSON_Specification
    282 GNUNET_JSON_spec_object_copy (const char *name,
    283                               json_t **jsonp);
    284 
    285 
    286 /**
    287  * JSON array, reference counter not incremented.
    288  *
    289  * @param name name of the JSON field
    290  * @param[out] jsonp where to store the JSON found under @a name
    291  */
    292 struct GNUNET_JSON_Specification
    293 GNUNET_JSON_spec_array_copy (const char *name,
    294                              json_t **jsonp);
    295 
    296 
    297 /**
    298  * JSON object, reference counter not incremented.
    299  *
    300  * @param name name of the JSON field
    301  * @param[out] jsonp where to store the JSON found under @a name
    302  */
    303 struct GNUNET_JSON_Specification
    304 GNUNET_JSON_spec_object_const (const char *name,
    305                                const json_t **jsonp);
    306 
    307 
    308 /**
    309  * JSON array, reference counter not incremented.
    310  *
    311  * @param name name of the JSON field
    312  * @param[out] jsonp where to store the JSON found under @a name
    313  */
    314 struct GNUNET_JSON_Specification
    315 GNUNET_JSON_spec_array_const (const char *name,
    316                               const json_t **jsonp);
    317 
    318 
    319 /**
    320  * boolean.
    321  *
    322  * @param name name of the JSON field
    323  * @param[out] b where to store the boolean found under @a name
    324  */
    325 struct GNUNET_JSON_Specification
    326 GNUNET_JSON_spec_bool (const char *name,
    327                        bool *b);
    328 
    329 
    330 /**
    331  * double.
    332  *
    333  * @param name name of the JSON field
    334  * @param[out] f where to store the double found under @a name
    335  */
    336 struct GNUNET_JSON_Specification
    337 GNUNET_JSON_spec_double (const char *name,
    338                          double *f);
    339 
    340 
    341 /**
    342  * 8-bit integer.
    343  *
    344  * @param name name of the JSON field
    345  * @param[out] u8 where to store the integer found under @a name
    346  */
    347 struct GNUNET_JSON_Specification
    348 GNUNET_JSON_spec_uint8 (const char *name,
    349                         uint8_t *u8);
    350 
    351 
    352 /**
    353  * 16-bit integer.
    354  *
    355  * @param name name of the JSON field
    356  * @param[out] u16 where to store the integer found under @a name
    357  */
    358 struct GNUNET_JSON_Specification
    359 GNUNET_JSON_spec_uint16 (const char *name,
    360                          uint16_t *u16);
    361 
    362 
    363 /**
    364  * 32-bit integer.
    365  *
    366  * @param name name of the JSON field
    367  * @param[out] u32 where to store the integer found under @a name
    368  */
    369 struct GNUNET_JSON_Specification
    370 GNUNET_JSON_spec_uint32 (const char *name,
    371                          uint32_t *u32);
    372 
    373 
    374 /**
    375  * Unsigned integer.
    376  *
    377  * @param name name of the JSON field
    378  * @param[out] ui where to store the integer found under @a name
    379  */
    380 struct GNUNET_JSON_Specification
    381 GNUNET_JSON_spec_uint (const char *name,
    382                        unsigned int *ui);
    383 
    384 /**
    385  * Unsigned long long.
    386  *
    387  * @param name name of the JSON field
    388  * @param[out] ull where to store the unsigned long long found under @a name
    389  */
    390 struct GNUNET_JSON_Specification
    391 GNUNET_JSON_spec_ull (const char *name,
    392                       unsigned long long *ull);
    393 
    394 
    395 /**
    396  * 64-bit integer.
    397  *
    398  * @param name name of the JSON field
    399  * @param[out] u64 where to store the integer found under @a name
    400  */
    401 struct GNUNET_JSON_Specification
    402 GNUNET_JSON_spec_uint64 (const char *name,
    403                          uint64_t *u64);
    404 
    405 
    406 /**
    407  * 16-bit signed integer.
    408  *
    409  * @param name name of the JSON field
    410  * @param[out] i16 where to store the integer found under @a name
    411  */
    412 struct GNUNET_JSON_Specification
    413 GNUNET_JSON_spec_int16 (const char *name,
    414                         int16_t *i16);
    415 
    416 /**
    417  * 64-bit signed integer.
    418  *
    419  * @param name name of the JSON field
    420  * @param[out] i64 where to store the integer found under @a name
    421  */
    422 struct GNUNET_JSON_Specification
    423 GNUNET_JSON_spec_int64 (const char *name,
    424                         int64_t *i64);
    425 
    426 
    427 /**
    428  * Boolean (true mapped to #GNUNET_YES, false mapped to #GNUNET_NO).
    429  *
    430  * @param name name of the JSON field
    431  * @param[out] boolean where to store the boolean found under @a name
    432  */
    433 struct GNUNET_JSON_Specification
    434 GNUNET_JSON_spec_boolean (const char *name,
    435                           int *boolean);
    436 
    437 
    438 /* ************ GNUnet-specific parser specifications ******************* */
    439 
    440 /**
    441  * Timestamp.
    442  *
    443  * @param name name of the JSON field
    444  * @param[out] t at where to store the absolute time found under @a name
    445  */
    446 struct GNUNET_JSON_Specification
    447 GNUNET_JSON_spec_timestamp (const char *name,
    448                             struct GNUNET_TIME_Timestamp *t);
    449 
    450 
    451 /**
    452  * Timestamp in network byte order.
    453  *
    454  * @param name name of the JSON field
    455  * @param[out] tn where to store the absolute time found under @a name
    456  */
    457 struct GNUNET_JSON_Specification
    458 GNUNET_JSON_spec_timestamp_nbo (const char *name,
    459                                 struct GNUNET_TIME_TimestampNBO *tn);
    460 
    461 
    462 /**
    463  * Relative time.
    464  *
    465  * @param name name of the JSON field
    466  * @param[out] rt where to store the relative time found under @a name
    467  */
    468 struct GNUNET_JSON_Specification
    469 GNUNET_JSON_spec_relative_time (const char *name,
    470                                 struct GNUNET_TIME_Relative *rt);
    471 
    472 
    473 /**
    474  * Specification for parsing an RSA public key.
    475  *
    476  * @param name name of the JSON field
    477  * @param pk where to store the RSA key found under @a name
    478  */
    479 struct GNUNET_JSON_Specification
    480 GNUNET_JSON_spec_rsa_public_key (const char *name,
    481                                  struct GNUNET_CRYPTO_RsaPublicKey **pk);
    482 
    483 
    484 /**
    485  * Specification for parsing an RSA signature.
    486  *
    487  * @param name name of the JSON field
    488  * @param sig where to store the RSA signature found under @a name
    489  */
    490 struct GNUNET_JSON_Specification
    491 GNUNET_JSON_spec_rsa_signature (const char *name,
    492                                 struct GNUNET_CRYPTO_RsaSignature **sig);
    493 
    494 
    495 /**
    496  * Specification for parsing a blinded message.
    497  *
    498  * @param name name of the JSON field
    499  * @param sig where to store the blinded message found under @a name
    500  */
    501 struct GNUNET_JSON_Specification
    502 GNUNET_JSON_spec_blinded_message (const char *name,
    503                                   struct GNUNET_CRYPTO_BlindedMessage **msg);
    504 
    505 
    506 /**
    507  * Specification for parsing a blinded signature.
    508  *
    509  * @param name name of the JSON field
    510  * @param sig where to store the blinded signature found under @a name
    511  */
    512 struct GNUNET_JSON_Specification
    513 GNUNET_JSON_spec_blinded_signature (
    514   const char *field,
    515   struct GNUNET_CRYPTO_BlindedSignature **b_sig);
    516 
    517 
    518 /**
    519  * Specification for parsing an unblinded signature.
    520  *
    521  * @param name name of the JSON field
    522  * @param sig where to store the unblinded signature found under @a name
    523  */
    524 struct GNUNET_JSON_Specification
    525 GNUNET_JSON_spec_unblinded_signature (
    526   const char *field,
    527   struct GNUNET_CRYPTO_UnblindedSignature **ub_sig);
    528 
    529 
    530 /* ****************** Generic generator interface ******************* */
    531 
    532 
    533 /**
    534  * Convert binary data to a JSON string with the base32crockford
    535  * encoding.
    536  *
    537  * @param data binary data
    538  * @param size size of @a data in bytes
    539  * @return json string that encodes @a data
    540  */
    541 json_t *
    542 GNUNET_JSON_from_data (const void *data,
    543                        size_t size);
    544 
    545 
    546 /**
    547  * Convert binary data to a JSON string with base64
    548  * encoding.
    549  *
    550  * @param data binary data
    551  * @param size size of @a data in bytes
    552  * @return json string that encodes @a data
    553  */
    554 json_t *
    555 GNUNET_JSON_from_data64 (const void *data,
    556                          size_t size);
    557 
    558 
    559 /**
    560  * Convert binary data to a JSON string with the base32crockford
    561  * encoding.
    562  *
    563  * @param ptr binary data, sizeof (*ptr) must yield correct size
    564  * @return json string that encodes @a data
    565  */
    566 #define GNUNET_JSON_from_data_auto(ptr) \
    567         GNUNET_JSON_from_data (ptr, sizeof(*(ptr)))
    568 
    569 
    570 /**
    571  * Convert binary data to a JSON string with base64
    572  * encoding.
    573  *
    574  * @param ptr binary data, sizeof (*ptr) must yield correct size
    575  * @return json string that encodes @a data
    576  */
    577 #define GNUNET_JSON_from_data64_auto(ptr) \
    578         GNUNET_JSON_from_data64 (ptr, sizeof(*(ptr)))
    579 
    580 
    581 /**
    582  * Convert timestamp to a json string.
    583  *
    584  * @param stamp the time stamp
    585  * @return a json string with the timestamp in @a stamp
    586  */
    587 json_t *
    588 GNUNET_JSON_from_timestamp (struct GNUNET_TIME_Timestamp stamp);
    589 
    590 
    591 /**
    592  * Convert timestamp to a json string.
    593  *
    594  * @param stamp the time stamp
    595  * @return a json string with the timestamp in @a stamp
    596  */
    597 json_t *
    598 GNUNET_JSON_from_timestamp_nbo (struct GNUNET_TIME_TimestampNBO stamp);
    599 
    600 
    601 /**
    602  * Convert relative timestamp to a json string.
    603  *
    604  * @param stamp the time stamp
    605  * @return a json string with the timestamp in @a stamp
    606  */
    607 json_t *
    608 GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp);
    609 
    610 
    611 /**
    612  * Convert RSA public key to JSON.
    613  *
    614  * @param pk public key to convert
    615  * @return corresponding JSON encoding
    616  */
    617 json_t *
    618 GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk);
    619 
    620 
    621 /**
    622  * Convert RSA signature to JSON.
    623  *
    624  * @param sig signature to convert
    625  * @return corresponding JSON encoding
    626  */
    627 json_t *
    628 GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig);
    629 
    630 /* ****************** GETOPT JSON helper ******************* */
    631 
    632 
    633 /**
    634  * Allow user to specify a JSON input value.
    635  *
    636  * @param shortName short name of the option
    637  * @param name long name of the option
    638  * @param argumentHelp help text for the option argument
    639  * @param description long help text for the option
    640  * @param[out] val set to the JSON specified at the command line
    641  */
    642 struct GNUNET_GETOPT_CommandLineOption
    643 GNUNET_JSON_getopt (char shortName,
    644                     const char *name,
    645                     const char *argumentHelp,
    646                     const char *description,
    647                     json_t **json);
    648 
    649 
    650 /* ****************** JSON PACK helper ******************* */
    651 
    652 
    653 /**
    654  * Element in the array to give to the packer.
    655  */
    656 struct GNUNET_JSON_PackSpec;
    657 
    658 
    659 /**
    660  * Function called to pack an element into the JSON
    661  * object as part of #GNUNET_JSON_pack_().
    662  *
    663  * @param se pack specification to execute
    664  * @return json object to pack, NULL to pack nothing
    665  */
    666 typedef json_t *
    667 (*GNUNET_JSON_PackCallback)(const struct GNUNET_JSON_PackSpec *se);
    668 
    669 
    670 /**
    671  * Element in the array to give to the packer.
    672  */
    673 struct GNUNET_JSON_PackSpec
    674 {
    675   /**
    676    * Name of the field to pack. If (and only if) @e object contains an actual
    677    * JSON object, NULL will pack the keys in the top level of the resulting
    678    * JSON object rather than under a field.
    679    */
    680   const char *field_name;
    681 
    682   /**
    683    * Object to pack.
    684    */
    685   json_t *object;
    686 
    687   /**
    688    * True if a NULL (or 0) argument is allowed. In this
    689    * case, if the argument is NULL the @e packer should
    690    * return NULL and the field should be skipped (omitted from
    691    * the generated object) and not be serialized at all.
    692    */
    693   bool allow_null;
    694 
    695   /**
    696    * True if last element in the spec array.
    697    */
    698   bool final;
    699 };
    700 
    701 
    702 /**
    703  * Pack a JSON object from a @a spec. Aborts if
    704  * packing fails.
    705  *
    706  * @param spec specification object
    707  * @return JSON object
    708  */
    709 json_t *
    710 GNUNET_JSON_pack_ (struct GNUNET_JSON_PackSpec spec[]);
    711 
    712 
    713 /**
    714  * Pack a JSON object from a @a spec. Aborts if
    715  * packing fails.
    716  *
    717  * @param ... list of specification objects
    718  * @return JSON object
    719  */
    720 #define GNUNET_JSON_PACK(...) \
    721         GNUNET_JSON_pack_ ((struct GNUNET_JSON_PackSpec[]) {__VA_ARGS__, \
    722                                                             GNUNET_JSON_pack_end_ ()})
    723 
    724 
    725 /**
    726  * Do not use directly. Use #GNUNET_JSON_PACK.
    727  *
    728  * @return array terminator
    729  */
    730 struct GNUNET_JSON_PackSpec
    731 GNUNET_JSON_pack_end_ (void);
    732 
    733 
    734 /**
    735  * Modify packer instruction to allow NULL as a value.
    736  *
    737  * @param in json pack specification to modify
    738  * @return json pack specification
    739  */
    740 struct GNUNET_JSON_PackSpec
    741 GNUNET_JSON_pack_allow_null (struct GNUNET_JSON_PackSpec in);
    742 
    743 
    744 /**
    745  * Generate packer instruction for a JSON field of type
    746  * bool.
    747  *
    748  * @param name name of the field to add to the object
    749  * @param b boolean value
    750  * @return json pack specification
    751  */
    752 struct GNUNET_JSON_PackSpec
    753 GNUNET_JSON_pack_bool (const char *name,
    754                        bool b);
    755 
    756 
    757 /**
    758  * Generate packer instruction for a JSON field of type
    759  * double.
    760  *
    761  * @param name name of the field to add to the object
    762  * @param f double value
    763  * @return json pack specification
    764  */
    765 struct GNUNET_JSON_PackSpec
    766 GNUNET_JSON_pack_double (const char *name,
    767                          double f);
    768 
    769 
    770 /**
    771  * Generate packer instruction for a JSON field of type
    772  * string.
    773  *
    774  * @param name name of the field to add to the object
    775  * @param s string value
    776  * @return json pack specification
    777  */
    778 struct GNUNET_JSON_PackSpec
    779 GNUNET_JSON_pack_string (const char *name,
    780                          const char *s);
    781 
    782 
    783 /**
    784  * Generate packer instruction for a JSON field of type
    785  * unsigned integer. Note that the maximum allowed
    786  * value is still limited by JSON and not UINT64_MAX.
    787  *
    788  * @param name name of the field to add to the object
    789  * @param num numeric value
    790  * @return json pack specification
    791  */
    792 struct GNUNET_JSON_PackSpec
    793 GNUNET_JSON_pack_uint64 (const char *name,
    794                          uint64_t num);
    795 
    796 
    797 /**
    798  * Generate packer instruction for a JSON field of type
    799  * signed integer.
    800  *
    801  * @param name name of the field to add to the object
    802  * @param num numeric value
    803  * @return json pack specification
    804  */
    805 struct GNUNET_JSON_PackSpec
    806 GNUNET_JSON_pack_int64 (const char *name,
    807                         int64_t num);
    808 
    809 
    810 /**
    811  * Generate packer instruction for a JSON field of type
    812  * JSON object where the reference is taken over by
    813  * the packer.
    814  *
    815  * @param name name of the field to add to the object, if NULL, the keys of
    816  *        @a o will be placed in the top level of the resulting object
    817  * @param o object to steal
    818  * @return json pack specification
    819  */
    820 struct GNUNET_JSON_PackSpec
    821 GNUNET_JSON_pack_object_steal (const char *name,
    822                                json_t *o);
    823 
    824 
    825 /**
    826  * Generate packer instruction for a JSON field of type JSON object where the
    827  * reference counter is incremented by the packer.  Note that a deep copy is
    828  * not performed.
    829  *
    830  * @param name name of the field to add to the object, if NULL, the keys of
    831  *        @a o will be placed in the top level of the resulting object
    832  * @param o object to increment reference counter of
    833  * @return json pack specification
    834  */
    835 struct GNUNET_JSON_PackSpec
    836 GNUNET_JSON_pack_object_incref (const char *name,
    837                                 json_t *o);
    838 
    839 
    840 /**
    841  * Generate packer instruction for a JSON field of type
    842  * JSON array where the reference is taken over by
    843  * the packer.
    844  *
    845  * @param name name of the field to add to the object
    846  * @param a array to steal
    847  * @return json pack specification
    848  */
    849 struct GNUNET_JSON_PackSpec
    850 GNUNET_JSON_pack_array_steal (const char *name,
    851                               json_t *a);
    852 
    853 
    854 /**
    855  * Generate packer instruction for a JSON field of type JSON array where the
    856  * reference counter is incremented by the packer.  Note that a deep copy is
    857  * not performed.
    858  *
    859  * @param name name of the field to add to the object
    860  * @param a array to increment reference counter of
    861  * @return json pack specification
    862  */
    863 struct GNUNET_JSON_PackSpec
    864 GNUNET_JSON_pack_array_incref (const char *name,
    865                                json_t *a);
    866 
    867 
    868 /**
    869  * Generate packer instruction for a JSON field of type
    870  * variable size binary blob.
    871  *
    872  * @param name name of the field to add to the object
    873  * @param blob binary data to pack
    874  * @param blob_size number of bytes in @a blob
    875  * @return json pack specification
    876  */
    877 struct GNUNET_JSON_PackSpec
    878 GNUNET_JSON_pack_data_varsize (const char *name,
    879                                const void *blob,
    880                                size_t blob_size);
    881 
    882 
    883 /**
    884  * Generate packer instruction for a JSON field where the
    885  * size is automatically determined from the argument.
    886  *
    887  * @param name name of the field to add to the object
    888  * @param blob data to pack, must not be an array
    889  * @return json pack specification
    890  */
    891 #define GNUNET_JSON_pack_data_auto(name,blob) \
    892         GNUNET_JSON_pack_data_varsize (name, (blob), sizeof (*(blob)))
    893 
    894 
    895 /**
    896  * Generate packer instruction for a JSON field of type
    897  * variable size binary blob.
    898  * Use base64-encoding, instead of the more common
    899  * Crockford base32-encoding.
    900  *
    901  * @param name name of the field to add to the object
    902  * @param blob binary data to pack
    903  * @param blob_size number of bytes in @a blob
    904  * @return json pack specification
    905  */
    906 struct GNUNET_JSON_PackSpec
    907 GNUNET_JSON_pack_data64_varsize (const char *name,
    908                                  const void *blob,
    909                                  size_t blob_size);
    910 
    911 
    912 /**
    913  * Generate packer instruction for a JSON field where the
    914  * size is automatically determined from the argument.
    915  * Use base64-encoding, instead of the more common
    916  * Crockford base32-encoding.
    917  *
    918  * @param name name of the field to add to the object
    919  * @param blob data to pack, must not be an array
    920  * @return json pack specification
    921  */
    922 #define GNUNET_JSON_pack_data64_auto(name,blob) \
    923         GNUNET_JSON_pack_data64_varsize (name, (blob), sizeof (*(blob)))
    924 
    925 
    926 /**
    927  * Generate packer instruction for a JSON field of type
    928  * timestamp.
    929  *
    930  * @param name name of the field to add to the object
    931  * @param at timestamp pack, a value of 0 is only
    932  *        allowed with #GNUNET_JSON_pack_allow_null()!
    933  * @return json pack specification
    934  */
    935 struct GNUNET_JSON_PackSpec
    936 GNUNET_JSON_pack_timestamp (const char *name,
    937                             struct GNUNET_TIME_Timestamp at);
    938 
    939 
    940 /**
    941  * Generate packer instruction for a JSON field of type
    942  * timestamp in network byte order.
    943  *
    944  * @param name name of the field to add to the object
    945  * @param at timestamp to pack, a value of 0 is only
    946  *        allowed with #GNUNET_JSON_pack_allow_null()!
    947  * @return json pack specification
    948  */
    949 struct GNUNET_JSON_PackSpec
    950 GNUNET_JSON_pack_timestamp_nbo (const char *name,
    951                                 struct GNUNET_TIME_TimestampNBO at);
    952 
    953 
    954 /**
    955  * Generate packer instruction for a JSON field of type
    956  * relative time.
    957  *
    958  * @param name name of the field to add to the object
    959  * @param rt relative time to pack
    960  * @return json pack specification
    961  */
    962 struct GNUNET_JSON_PackSpec
    963 GNUNET_JSON_pack_time_rel (const char *name,
    964                            struct GNUNET_TIME_Relative rt);
    965 
    966 
    967 /**
    968  * Generate packer instruction for a JSON field of type
    969  * relative time in network byte order.
    970  *
    971  * @param name name of the field to add to the object
    972  * @param rt relative time to pack
    973  * @return json pack specification
    974  */
    975 struct GNUNET_JSON_PackSpec
    976 GNUNET_JSON_pack_time_rel_nbo (const char *name,
    977                                struct GNUNET_TIME_RelativeNBO rt);
    978 
    979 
    980 /**
    981  * Generate packer instruction for a JSON field of type
    982  * RSA public key.
    983  *
    984  * @param name name of the field to add to the object
    985  * @param pk RSA public key
    986  * @return json pack specification
    987  */
    988 struct GNUNET_JSON_PackSpec
    989 GNUNET_JSON_pack_rsa_public_key (const char *name,
    990                                  const struct GNUNET_CRYPTO_RsaPublicKey *pk);
    991 
    992 
    993 /**
    994  * Generate packer instruction for a JSON field of type
    995  * RSA signature.
    996  *
    997  * @param name name of the field to add to the object
    998  * @param sig RSA signature
    999  * @return json pack specification
   1000  */
   1001 struct GNUNET_JSON_PackSpec
   1002 GNUNET_JSON_pack_rsa_signature (const char *name,
   1003                                 const struct GNUNET_CRYPTO_RsaSignature *sig);
   1004 
   1005 
   1006 /**
   1007  * Generate packer instruction for a JSON field of type
   1008  * unblinded signature.
   1009  *
   1010  * @param name name of the field to add to the object
   1011  * @param sig unblinded signature
   1012  * @return json pack specification
   1013  */
   1014 struct GNUNET_JSON_PackSpec
   1015 GNUNET_JSON_pack_unblinded_signature (
   1016   const char *name,
   1017   const struct GNUNET_CRYPTO_UnblindedSignature *sig);
   1018 
   1019 
   1020 /**
   1021  * Generate packer instruction for a JSON field of type
   1022  * blinded message.
   1023  *
   1024  * @param name name of the field to add to the object
   1025  * @param msg blinded message
   1026  * @return json pack specification
   1027  */
   1028 struct GNUNET_JSON_PackSpec
   1029 GNUNET_JSON_pack_blinded_message (
   1030   const char *name,
   1031   const struct GNUNET_CRYPTO_BlindedMessage *msg);
   1032 
   1033 
   1034 /**
   1035  * Generate packer instruction for a JSON field of type
   1036  * blinded signature.
   1037  *
   1038  * @param name name of the field to add to the object
   1039  * @param sig blinded signature
   1040  * @return json pack specification
   1041  */
   1042 struct GNUNET_JSON_PackSpec
   1043 GNUNET_JSON_pack_blinded_sig (const char *name,
   1044                               const struct GNUNET_CRYPTO_BlindedSignature *sig);
   1045 
   1046 #endif
   1047 
   1048 /* end of gnunet_json_lib.h */