diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-03-19 15:23:11 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-03-19 15:23:11 +0100 |
commit | 0d1eced630f4ed05ad95fdbb4354fd428c9cdbf6 (patch) | |
tree | 0ed4d6f189ed72287a0e003446c87a2b6db70c1a /src/bank-lib | |
parent | d229f78da3b0b7f851d3541f59651b4f600c879d (diff) |
first refactoring of JSON logic to address #4150 and #4237
Diffstat (limited to 'src/bank-lib')
-rw-r--r-- | src/bank-lib/Makefile.am | 3 | ||||
-rw-r--r-- | src/bank-lib/bank_api_admin.c | 7 | ||||
-rw-r--r-- | src/bank-lib/bank_api_context.h | 1 | ||||
-rw-r--r-- | src/bank-lib/bank_api_json.c | 525 | ||||
-rw-r--r-- | src/bank-lib/bank_api_json.h | 352 |
5 files changed, 7 insertions, 881 deletions
diff --git a/src/bank-lib/Makefile.am b/src/bank-lib/Makefile.am index 2f44adada..4cf3472ec 100644 --- a/src/bank-lib/Makefile.am +++ b/src/bank-lib/Makefile.am @@ -15,10 +15,11 @@ libtalerbank_la_LDFLAGS = \ libtalerbank_la_SOURCES = \ bank_api_context.c bank_api_context.h \ - bank_api_json.c bank_api_json.h \ bank_api_admin.c libtalerbank_la_LIBADD = \ + $(top_builddir)/src/json/libtalerjson.la \ + -lgnunetjson \ -lgnunetutil \ -ljansson \ $(XLIB) diff --git a/src/bank-lib/bank_api_admin.c b/src/bank-lib/bank_api_admin.c index 5deb4aa72..443f24a8a 100644 --- a/src/bank-lib/bank_api_admin.c +++ b/src/bank-lib/bank_api_admin.c @@ -24,8 +24,9 @@ #include <jansson.h> #include <microhttpd.h> /* just for HTTP status codes */ #include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_json_lib.h> #include "taler_bank_service.h" -#include "bank_api_json.h" +#include "taler_json_lib.h" #include "bank_api_context.h" #include "taler_signatures.h" @@ -173,9 +174,9 @@ TALER_BANK_admin_add_incoming (struct TALER_BANK_Context *bank, admin_obj = json_pack ("{s:o, s:o," " s:I, s:I}", - "wtid", TALER_json_from_data (wtid, + "wtid", GNUNET_JSON_from_data (wtid, sizeof (*wtid)), - "amount", TALER_json_from_amount (amount), + "amount", TALER_JSON_from_amount (amount), "debit_account", (json_int_t) debit_account_no, "credit_account", (json_int_t) credit_account_no); aai = GNUNET_new (struct TALER_BANK_AdminAddIncomingHandle); diff --git a/src/bank-lib/bank_api_context.h b/src/bank-lib/bank_api_context.h index 552cbe440..1f21cb8d2 100644 --- a/src/bank-lib/bank_api_context.h +++ b/src/bank-lib/bank_api_context.h @@ -21,6 +21,7 @@ * @author Christian Grothoff */ #include "platform.h" +#include <jansson.h> #include <curl/curl.h> #include <gnunet/gnunet_util_lib.h> #include "taler_bank_service.h" diff --git a/src/bank-lib/bank_api_json.c b/src/bank-lib/bank_api_json.c deleted file mode 100644 index 2a09e5272..000000000 --- a/src/bank-lib/bank_api_json.c +++ /dev/null @@ -1,525 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014, 2015 GNUnet e.V. - - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - TALER is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file bank-lib/bank_api_json.c - * @brief functions to parse incoming requests (JSON snippets) - * @author Florian Dold - * @author Benedikt Mueller - * @author Christian Grothoff - */ -#include "platform.h" -#include "bank_api_json.h" - -/** - * Navigate and parse data in a JSON tree. - * - * @param root the JSON node to start the navigation at. - * @param spec parse specification array - * @return offset in @a spec where parsing failed, -1 on success (!) - */ -static int -parse_json (json_t *root, - struct BAJ_Specification *spec) -{ - int i; - json_t *pos; /* what's our current position? */ - - pos = root; - for (i=0;BAJ_CMD_END != spec[i].cmd;i++) - { - pos = json_object_get (root, - spec[i].field); - if (NULL == pos) - { - GNUNET_break_op (0); - return i; - } - switch (spec[i].cmd) - { - case BAJ_CMD_END: - GNUNET_assert (0); - return i; - case BAJ_CMD_AMOUNT: - if (GNUNET_OK != - TALER_json_to_amount (pos, - spec[i].details.amount)) - { - GNUNET_break_op (0); - return i; - } - break; - case BAJ_CMD_TIME_ABSOLUTE: - if (GNUNET_OK != - TALER_json_to_abs (pos, - spec[i].details.abs_time)) - { - GNUNET_break_op (0); - return i; - } - break; - - case BAJ_CMD_STRING: - { - const char *str; - - str = json_string_value (pos); - if (NULL == str) - { - GNUNET_break_op (0); - return i; - } - *spec[i].details.strptr = str; - } - break; - - case BAJ_CMD_BINARY_FIXED: - { - const char *str; - int res; - - str = json_string_value (pos); - if (NULL == str) - { - GNUNET_break_op (0); - return i; - } - res = GNUNET_STRINGS_string_to_data (str, strlen (str), - spec[i].details.fixed_data.dest, - spec[i].details.fixed_data.dest_size); - if (GNUNET_OK != res) - { - GNUNET_break_op (0); - return i; - } - } - break; - - case BAJ_CMD_BINARY_VARIABLE: - { - const char *str; - size_t size; - void *data; - int res; - - str = json_string_value (pos); - if (NULL == str) - { - GNUNET_break_op (0); - return i; - } - size = (strlen (str) * 5) / 8; - if (size >= 1024) - { - GNUNET_break_op (0); - return i; - } - data = GNUNET_malloc (size); - res = GNUNET_STRINGS_string_to_data (str, - strlen (str), - data, - size); - if (GNUNET_OK != res) - { - GNUNET_break_op (0); - GNUNET_free (data); - return i; - } - *spec[i].details.variable_data.dest_p = data; - *spec[i].details.variable_data.dest_size_p = size; - } - break; - - case BAJ_CMD_RSA_PUBLIC_KEY: - { - size_t size; - const char *str; - int res; - void *buf; - - str = json_string_value (pos); - if (NULL == str) - { - GNUNET_break_op (0); - return i; - } - size = (strlen (str) * 5) / 8; - buf = GNUNET_malloc (size); - res = GNUNET_STRINGS_string_to_data (str, - strlen (str), - buf, - size); - if (GNUNET_OK != res) - { - GNUNET_free (buf); - GNUNET_break_op (0); - return i; - } - *spec[i].details.rsa_public_key - = GNUNET_CRYPTO_rsa_public_key_decode (buf, - size); - GNUNET_free (buf); - if (NULL == spec[i].details.rsa_public_key) - { - GNUNET_break_op (0); - return i; - } - } - break; - - case BAJ_CMD_RSA_SIGNATURE: - { - size_t size; - const char *str; - int res; - void *buf; - - str = json_string_value (pos); - if (NULL == str) - { - GNUNET_break_op (0); - return i; - } - size = (strlen (str) * 5) / 8; - buf = GNUNET_malloc (size); - res = GNUNET_STRINGS_string_to_data (str, - strlen (str), - buf, - size); - if (GNUNET_OK != res) - { - GNUNET_free (buf); - GNUNET_break_op (0); - return i; - } - *spec[i].details.rsa_signature - = GNUNET_CRYPTO_rsa_signature_decode (buf, - size); - GNUNET_free (buf); - if (NULL == spec[i].details.rsa_signature) - return i; - } - break; - - case BAJ_CMD_UINT16: - { - json_int_t val; - - if (! json_is_integer (pos)) - { - GNUNET_break_op (0); - return i; - } - val = json_integer_value (pos); - if ( (0 > val) || (val > UINT16_MAX) ) - { - GNUNET_break_op (0); - return i; - } - *spec[i].details.u16 = (uint16_t) val; - } - break; - - case BAJ_CMD_UINT64: - { - json_int_t val; - - if (! json_is_integer (pos)) - { - GNUNET_break_op (0); - return i; - } - val = json_integer_value (pos); - *spec[i].details.u64 = (uint64_t) val; - } - break; - - case BAJ_CMD_JSON_OBJECT: - { - if (! (json_is_object (pos) || json_is_array (pos)) ) - { - GNUNET_break_op (0); - return i; - } - json_incref (pos); - *spec[i].details.obj = pos; - } - break; - - default: - GNUNET_break (0); - return i; - } - } - return -1; /* all OK! */ -} - - -/** - * Free all elements allocated during a - * #BAJ_parse_json() operation. - * - * @param spec specification of the parse operation - * @param end number of elements in @a spec to process - */ -static void -parse_free (struct BAJ_Specification *spec, - int end) -{ - int i; - - for (i=0;i<end;i++) - { - switch (spec[i].cmd) - { - case BAJ_CMD_END: - GNUNET_assert (0); - return; - case BAJ_CMD_AMOUNT: - break; - case BAJ_CMD_TIME_ABSOLUTE: - break; - case BAJ_CMD_BINARY_FIXED: - break; - case BAJ_CMD_STRING: - break; - case BAJ_CMD_BINARY_VARIABLE: - GNUNET_free (*spec[i].details.variable_data.dest_p); - *spec[i].details.variable_data.dest_p = NULL; - *spec[i].details.variable_data.dest_size_p = 0; - break; - case BAJ_CMD_RSA_PUBLIC_KEY: - GNUNET_CRYPTO_rsa_public_key_free (*spec[i].details.rsa_public_key); - *spec[i].details.rsa_public_key = NULL; - break; - case BAJ_CMD_RSA_SIGNATURE: - GNUNET_CRYPTO_rsa_signature_free (*spec[i].details.rsa_signature); - *spec[i].details.rsa_signature = NULL; - break; - case BAJ_CMD_JSON_OBJECT: - json_decref (*spec[i].details.obj); - *spec[i].details.obj = NULL; - break; - default: - GNUNET_break (0); - break; - } - } -} - - -/** - * Navigate and parse data in a JSON tree. - * - * @param root the JSON node to start the navigation at. - * @param spec parse specification array - * @return #GNUNET_OK on success, #GNUNET_SYSERR on error - */ -int -BAJ_parse_json (const json_t *root, - struct BAJ_Specification *spec) -{ - int ret; - - ret = parse_json ((json_t *) root, - spec); - if (-1 == ret) - return GNUNET_OK; - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "JSON field `%s` (%d) had unexpected value\n", - spec[ret].field, - ret); - parse_free (spec, ret); - return GNUNET_SYSERR; -} - - -/** - * Free all elements allocated during a - * #BAJ_parse_json() operation. - * - * @param spec specification of the parse operation - */ -void -BAJ_parse_free (struct BAJ_Specification *spec) -{ - int i; - - for (i=0;BAJ_CMD_END != spec[i].cmd;i++) ; - parse_free (spec, i); -} - - -/** - * The expected field stores a string. - * - * @param name name of the JSON field - * @param strptr where to store a pointer to the field - */ -struct BAJ_Specification -BAJ_spec_string (const char *name, - const char **strptr) -{ - struct BAJ_Specification ret = - { - .cmd = BAJ_CMD_STRING, - .field = name, - .details.strptr = strptr - }; - return ret; -} - - -/** - * Specification for parsing an absolute time value. - * - * @param name name of the JSON field - * @param at where to store the absolute time found under @a name - */ -struct BAJ_Specification -BAJ_spec_absolute_time (const char *name, - struct GNUNET_TIME_Absolute *at) -{ - struct BAJ_Specification ret = - { - .cmd = BAJ_CMD_TIME_ABSOLUTE, - .field = name, - .details.abs_time = at - }; - return ret; -} - - -/** - * Specification for parsing an amount value. - * - * @param name name of the JSON field - * @param amount where to store the amount found under @a name - */ -struct BAJ_Specification -BAJ_spec_amount (const char *name, - struct TALER_Amount *amount) -{ - struct BAJ_Specification ret = - { - .cmd = BAJ_CMD_AMOUNT, - .field = name, - .details.amount = amount - }; - return ret; -} - - -/** - * 16-bit integer. - * - * @param name name of the JSON field - * @param[out] u16 where to store the integer found under @a name - */ -struct BAJ_Specification -BAJ_spec_uint16 (const char *name, - uint16_t *u16) -{ - struct BAJ_Specification ret = - { - .cmd = BAJ_CMD_UINT16, - .field = name, - .details.u16 = u16 - }; - return ret; -} - - -/** - * 64-bit integer. - * - * @param name name of the JSON field - * @param[out] u64 where to store the integer found under @a name - */ -struct BAJ_Specification -BAJ_spec_uint64 (const char *name, - uint64_t *u64) -{ - struct BAJ_Specification ret = - { - .cmd = BAJ_CMD_UINT64, - .field = name, - .details.u64 = u64 - }; - return ret; -} - - -/** - * JSON object. - * - * @param name name of the JSON field - * @param[out] jsonp where to store the JSON found under @a name - */ -struct BAJ_Specification -BAJ_spec_json (const char *name, - json_t **jsonp) -{ - struct BAJ_Specification ret = - { - .cmd = BAJ_CMD_JSON_OBJECT, - .field = name, - .details.obj = jsonp - }; - return ret; -} - - -/** - * Specification for parsing an RSA public key. - * - * @param name name of the JSON field - * @param pk where to store the RSA key found under @a name - */ -struct BAJ_Specification -BAJ_spec_rsa_public_key (const char *name, - struct GNUNET_CRYPTO_rsa_PublicKey **pk) -{ - struct BAJ_Specification ret = - { - .cmd = BAJ_CMD_RSA_PUBLIC_KEY, - .field = name, - .details.rsa_public_key = pk - }; - return ret; -} - - -/** - * Specification for parsing an RSA signature. - * - * @param name name of the JSON field - * @param sig where to store the RSA signature found under @a name - */ -struct BAJ_Specification -BAJ_spec_rsa_signature (const char *name, - struct GNUNET_CRYPTO_rsa_Signature **sig) -{ - struct BAJ_Specification ret = - { - .cmd = BAJ_CMD_RSA_SIGNATURE, - .field = name, - .details.rsa_signature = sig - }; - return ret; -} - - -/* end of bank_api_json.c */ diff --git a/src/bank-lib/bank_api_json.h b/src/bank-lib/bank_api_json.h deleted file mode 100644 index 92fe0b1d4..000000000 --- a/src/bank-lib/bank_api_json.h +++ /dev/null @@ -1,352 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014, 2015 GNUnet e.V. - - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - TALER is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file exchange-lib/exchange_api_json.h - * @brief functions to parse incoming requests (JSON snippets) - * @author Florian Dold - * @author Benedikt Mueller - * @author Christian Grothoff - */ -#include "platform.h" -#include <gnunet/gnunet_util_lib.h> -#include "taler_util.h" -#include <jansson.h> - - -/** - * Enumeration with the various commands for the - * #BAJ_parse_json interpreter. - */ -enum BAJ_Command -{ - - /** - * End of command list. - */ - BAJ_CMD_END, - - /** - * Parse amount at current position. - */ - BAJ_CMD_AMOUNT, - - /** - * Parse absolute time at current position. - */ - BAJ_CMD_TIME_ABSOLUTE, - - /** - * Parse fixed binary value at current position. - */ - BAJ_CMD_BINARY_FIXED, - - /** - * Parse variable-size binary value at current position. - */ - BAJ_CMD_BINARY_VARIABLE, - - /** - * Parse RSA public key at current position. - */ - BAJ_CMD_RSA_PUBLIC_KEY, - - /** - * Parse RSA signature at current position. - */ - BAJ_CMD_RSA_SIGNATURE, - - /** - * Parse `const char *` JSON string at current position. - */ - BAJ_CMD_STRING, - - /** - * Parse `uint16_t` integer at the current position. - */ - BAJ_CMD_UINT16, - - /** - * Parse `uint64_t` integer at the current position. - */ - BAJ_CMD_UINT64, - - /** - * Parse JSON object at the current position. - */ - BAJ_CMD_JSON_OBJECT, - - /** - * Parse ??? at current position. - */ - BAJ_CMD_C - -}; - - -/** - * @brief Entry in parser specification for #BAJ_parse_json. - */ -struct BAJ_Specification -{ - - /** - * Command to execute. - */ - enum BAJ_Command cmd; - - /** - * Name of the field to access. - */ - const char *field; - - /** - * Further details for the command. - */ - union { - - /** - * Where to store amount for #BAJ_CMD_AMOUNT. - */ - struct TALER_Amount *amount; - - /** - * Where to store time, for #BAJ_CMD_TIME_ABSOLUTE. - */ - struct GNUNET_TIME_Absolute *abs_time; - - /** - * Where to write binary data, for #BAJ_CMD_BINARY_FIXED. - */ - struct { - /** - * Where to write the data. - */ - void *dest; - - /** - * How many bytes to write to @e dest. - */ - size_t dest_size; - - } fixed_data; - - /** - * Where to write binary data, for #BAJ_CMD_BINARY_VARIABLE. - */ - struct { - /** - * Where to store the pointer with the data (is allocated). - */ - void **dest_p; - - /** - * Where to store the number of bytes allocated at `*dest`. - */ - size_t *dest_size_p; - - } variable_data; - - /** - * Where to store the RSA public key for #BAJ_CMD_RSA_PUBLIC_KEY - */ - struct GNUNET_CRYPTO_rsa_PublicKey **rsa_public_key; - - /** - * Where to store the RSA signature for #BAJ_CMD_RSA_SIGNATURE - */ - struct GNUNET_CRYPTO_rsa_Signature **rsa_signature; - - /** - * Details for #BAJ_CMD_EDDSA_SIGNATURE - */ - struct { - - /** - * Where to store the purpose. - */ - struct GNUNET_CRYPTO_EccSignaturePurpose **purpose_p; - - /** - * Key to verify the signature against. - */ - const struct GNUNET_CRYPTO_EddsaPublicKey *pub_key; - - } eddsa_signature; - - /** - * Where to store a pointer to the string. - */ - const char **strptr; - - /** - * Where to store 16-bit integer. - */ - uint16_t *u16; - - /** - * Where to store 64-bit integer. - */ - uint64_t *u64; - - /** - * Where to store a JSON object. - */ - json_t **obj; - - } details; - -}; - - -/** - * Navigate and parse data in a JSON tree. - * - * @param root the JSON node to start the navigation at. - * @param spec parse specification array - * @return #GNUNET_OK on success, #GNUNET_SYSERR on error - */ -int -BAJ_parse_json (const json_t *root, - struct BAJ_Specification *spec); - - -/** - * Free all elements allocated during a - * #BAJ_parse_json() operation. - * - * @param spec specification of the parse operation - */ -void -BAJ_parse_free (struct BAJ_Specification *spec); - - -/** - * End of a parser specification. - */ -#define BAJ_spec_end { .cmd = BAJ_CMD_END } - -/** - * Fixed size object (in network byte order, encoded using Crockford - * Base32hex encoding). - * - * @param name name of the JSON field - * @param obj pointer where to write the data (type of `*obj` will determine size) - */ -#define BAJ_spec_fixed_auto(name,obj) { .cmd = BAJ_CMD_BINARY_FIXED, .field = name, .details.fixed_data.dest = obj, .details.fixed_data.dest_size = sizeof (*obj) } - - -/** - * Variable size object (in network byte order, encoded using Crockford - * Base32hex encoding). - * - * @param name name of the JSON field - * @param obj pointer where to write the data (a `void **`) - * @param size where to store the number of bytes allocated for @a obj (of type `size_t *` - */ -#define BAJ_spec_varsize(name,obj,size) { .cmd = BAJ_CMD_BINARY_VARIABLE, .field = name, .details.variable_data.dest_p = obj, .details.variable_data.dest_size_p = size } - - -/** - * The expected field stores a string. - * - * @param name name of the JSON field - * @param strptr where to store a pointer to the field - */ -struct BAJ_Specification -BAJ_spec_string (const char *name, - const char **strptr); - - -/** - * Absolute time. - * - * @param name name of the JSON field - * @param[out] at where to store the absolute time found under @a name - */ -struct BAJ_Specification -BAJ_spec_absolute_time (const char *name, - struct GNUNET_TIME_Absolute *at); - - -/** - * 16-bit integer. - * - * @param name name of the JSON field - * @param[out] u16 where to store the integer found under @a name - */ -struct BAJ_Specification -BAJ_spec_uint16 (const char *name, - uint16_t *u16); - - -/** - * 64-bit integer. - * - * @param name name of the JSON field - * @param[out] u64 where to store the integer found under @a name - */ -struct BAJ_Specification -BAJ_spec_uint64 (const char *name, - uint64_t *u64); - - -/** - * JSON object. - * - * @param name name of the JSON field - * @param[out] jsonp where to store the JSON found under @a name - */ -struct BAJ_Specification -BAJ_spec_json (const char *name, - json_t **jsonp); - - -/** - * Specification for parsing an amount value. - * - * @param name name of the JSON field - * @param amount where to store the amount under @a name - */ -struct BAJ_Specification -BAJ_spec_amount (const char *name, - struct TALER_Amount *amount); - - -/** - * Specification for parsing an RSA public key. - * - * @param name name of the JSON field - * @param pk where to store the RSA key found under @a name - */ -struct BAJ_Specification -BAJ_spec_rsa_public_key (const char *name, - struct GNUNET_CRYPTO_rsa_PublicKey **pk); - - -/** - * Specification for parsing an RSA signature. - * - * @param name name of the JSON field - * @param sig where to store the RSA signature found under @a name - */ -struct BAJ_Specification -BAJ_spec_rsa_signature (const char *name, - struct GNUNET_CRYPTO_rsa_Signature **sig); - - - - -/* end of exchange_api_json.h */ |