aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-03-19 15:23:11 +0100
committerChristian Grothoff <christian@grothoff.org>2016-03-19 15:23:11 +0100
commit0d1eced630f4ed05ad95fdbb4354fd428c9cdbf6 (patch)
tree0ed4d6f189ed72287a0e003446c87a2b6db70c1a /src/util
parentd229f78da3b0b7f851d3541f59651b4f600c879d (diff)
first refactoring of JSON logic to address #4150 and #4237
Diffstat (limited to 'src/util')
-rw-r--r--src/util/Makefile.am20
-rw-r--r--src/util/json.c400
-rw-r--r--src/util/test_json.c182
-rw-r--r--src/util/util.c42
-rw-r--r--src/util/wireformats.c441
5 files changed, 5 insertions, 1080 deletions
diff --git a/src/util/Makefile.am b/src/util/Makefile.am
index 8efc3987a..41afcc97f 100644
--- a/src/util/Makefile.am
+++ b/src/util/Makefile.am
@@ -1,5 +1,5 @@
# This Makefile.am is in the public domain
-AM_CPPFLAGS = -I$(top_srcdir)/src/include $(LIBGCRYPT_CFLAGS) $(POSTGRESQL_CPPFLAGS)
+AM_CPPFLAGS = -I$(top_srcdir)/src/include $(LIBGCRYPT_CFLAGS)
if USE_COVERAGE
AM_CFLAGS = --coverage -O0
@@ -31,15 +31,12 @@ libtalerutil_la_SOURCES = \
amount.c \
crypto.c \
util.c \
- json.c \
os_installation.c \
- plugin.c \
- wireformats.c
+ plugin.c
libtalerutil_la_LIBADD = \
-lgnunetutil \
$(LIBGCRYPT_LIBS) \
- -ljansson \
-lmicrohttpd $(XLIB)
libtalerutil_la_LDFLAGS = \
@@ -48,13 +45,11 @@ libtalerutil_la_LDFLAGS = \
TESTS = \
test_amount \
- test_crypto \
- test_json
+ test_crypto
check_PROGRAMS= \
test_amount \
- test_crypto \
- test_json
+ test_crypto
test_amount_SOURCES = \
@@ -68,10 +63,3 @@ test_crypto_SOURCES = \
test_crypto_LDADD = \
-lgnunetutil \
libtalerutil.la
-
-test_json_SOURCES = \
- test_json.c
-test_json_LDADD = \
- -lgnunetutil \
- -ljansson \
- libtalerutil.la
diff --git a/src/util/json.c b/src/util/json.c
deleted file mode 100644
index 6aca7548c..000000000
--- a/src/util/json.c
+++ /dev/null
@@ -1,400 +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 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 General Public License for more details.
-
- You should have received a copy of the GNU General Public License along with
- TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
-*/
-/**
- * @file util/json.c
- * @brief helper functions for JSON processing using libjansson
- * @author Sree Harsha Totakura <sreeharsha@totakura.in>
- */
-#include "platform.h"
-#if HAVE_GNUNET_GNUNET_UTIL_TALER_WALLET_LIB_H
-#include <gnunet/gnunet_util_taler_wallet_lib.h>
-#endif
-#if HAVE_GNUNET_GNUNET_UTIL_LIB_H
-#include <gnunet/gnunet_util_lib.h>
-#endif
-#include "taler_util.h"
-
-/**
- * Shorthand for exit jumps.
- */
-#define EXITIF(cond) \
- do { \
- if (cond) { GNUNET_break (0); goto EXITIF_exit; } \
- } while (0)
-
-/**
- * Shorthand for JSON parsing related exit jumps.
- */
-#define UNPACK_EXITIF(cond) \
- do { \
- if (cond) { TALER_json_warn (error); goto EXITIF_exit; } \
- } while (0)
-
-
-/**
- * Convert a TALER amount to a JSON
- * object.
- *
- * @param amount the amount
- * @return a json object describing the amount
- */
-json_t *
-TALER_json_from_amount (const struct TALER_Amount *amount)
-{
- json_t *j;
-
- if ( (amount->value != (uint64_t) ((json_int_t) amount->value)) ||
- (0 > ((json_int_t) amount->value)) )
- {
- /* Theoretically, json_int_t can be a 32-bit "long", or we might
- have a 64-bit value which converted to a 63-bit signed long
- long causes problems here. So we check. Note that depending
- on the platform, the compiler may be able to statically tell
- that at least the first check is always false. */
- GNUNET_break (0);
- return NULL;
- }
- j = json_pack ("{s:s, s:I, s:I}",
- "currency", amount->currency,
- "value", (json_int_t) amount->value,
- "fraction", (json_int_t) amount->fraction);
- GNUNET_assert (NULL != j);
- return j;
-}
-
-
-/**
- * Convert absolute timestamp to a json string.
- *
- * @param stamp the time stamp
- * @return a json string with the timestamp in @a stamp
- */
-json_t *
-TALER_json_from_abs (struct GNUNET_TIME_Absolute stamp)
-{
- json_t *j;
- char *mystr;
- int ret;
-
- GNUNET_assert (GNUNET_OK ==
- TALER_round_abs_time (&stamp));
- if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
- return json_string ("/never/");
- ret = GNUNET_asprintf (&mystr,
- "/Date(%llu)/",
- (unsigned long long) (stamp.abs_value_us / (1000LL * 1000LL)));
- GNUNET_assert (ret > 0);
- j = json_string (mystr);
- GNUNET_free (mystr);
- return j;
-}
-
-
-/**
- * Convert RSA public key to JSON.
- *
- * @param pk public key to convert
- * @return corresponding JSON encoding
- */
-json_t *
-TALER_json_from_rsa_public_key (struct GNUNET_CRYPTO_rsa_PublicKey *pk)
-{
- char *buf;
- size_t buf_len;
- json_t *ret;
-
- buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
- &buf);
- ret = TALER_json_from_data (buf,
- buf_len);
- GNUNET_free (buf);
- return ret;
-}
-
-
-/**
- * Convert JSON to RSA public key.
- *
- * @param json JSON encoding to convert
- * @return corresponding public key
- */
-struct GNUNET_CRYPTO_rsa_PublicKey *
-TALER_json_to_rsa_public_key (json_t *json)
-{
- const char *enc;
- char *buf;
- size_t len;
- size_t buf_len;
- struct GNUNET_CRYPTO_rsa_PublicKey *pk;
-
- buf = NULL;
- EXITIF (NULL == (enc = json_string_value (json)));
- len = strlen (enc);
- buf_len = (len * 5) / 8;
- buf = GNUNET_malloc (buf_len);
- EXITIF (GNUNET_OK !=
- GNUNET_STRINGS_string_to_data (enc,
- len,
- buf,
- buf_len));
- EXITIF (NULL == (pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
- buf_len)));
- GNUNET_free (buf);
- return pk;
- EXITIF_exit:
- GNUNET_free_non_null (buf);
- return NULL;
-}
-
-
-/**
- * Convert JSON to RSA signature.
- *
- * @param json JSON encoding to convert
- * @return corresponding signature
- */
-struct GNUNET_CRYPTO_rsa_Signature *
-TALER_json_to_rsa_signature (json_t *json)
-{
- const char *enc;
- char *buf;
- size_t len;
- size_t buf_len;
- struct GNUNET_CRYPTO_rsa_Signature *sig;
-
- buf = NULL;
- EXITIF (NULL == (enc = json_string_value (json)));
- len = strlen (enc);
- buf_len = (len * 5) / 8;
- buf = GNUNET_malloc (buf_len);
- EXITIF (GNUNET_OK !=
- GNUNET_STRINGS_string_to_data (enc,
- len,
- buf,
- buf_len));
- EXITIF (NULL == (sig = GNUNET_CRYPTO_rsa_signature_decode (buf,
- buf_len)));
- GNUNET_free (buf);
- return sig;
- EXITIF_exit:
- GNUNET_free_non_null (buf);
- return NULL;
-}
-
-
-/**
- * Convert RSA signature to JSON.
- *
- * @param sig signature to convert
- * @return corresponding JSON encoding
- */
-json_t *
-TALER_json_from_rsa_signature (struct GNUNET_CRYPTO_rsa_Signature *sig)
-{
- char *buf;
- size_t buf_len;
- json_t *ret;
-
- buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
- &buf);
- ret = TALER_json_from_data (buf,
- buf_len);
- GNUNET_free (buf);
- return ret;
-}
-
-
-/**
- * Convert binary data to a JSON string
- * with the base32crockford encoding.
- *
- * @param data binary data
- * @param size size of @a data in bytes
- * @return json string that encodes @a data
- */
-json_t *
-TALER_json_from_data (const void *data,
- size_t size)
-{
- char *buf;
- json_t *json;
-
- buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
- json = json_string (buf);
- GNUNET_free (buf);
- return json;
-}
-
-
-/**
- * Parse given JSON object to Amount
- *
- * @param json the json object representing Amount
- * @param[out] r_amount where the amount has to be written
- * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
- */
-int
-TALER_json_to_amount (json_t *json,
- struct TALER_Amount *r_amount)
-{
- json_int_t value;
- json_int_t fraction;
- const char *currency;
-
- memset (r_amount,
- 0,
- sizeof (struct TALER_Amount));
- if (0 != json_unpack (json,
- "{s:I, s:I, s:s}",
- "value", &value,
- "fraction", &fraction,
- "currency", &currency))
- {
- char *json_enc;
-
- if (NULL == (json_enc = json_dumps (json,
- JSON_COMPACT | JSON_ENCODE_ANY)))
- {
- GNUNET_break (0);
- return GNUNET_SYSERR;
- }
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- "Malformed JSON amount: %s\n",
- json_enc);
- free (json_enc);
- return GNUNET_SYSERR;
- }
- if ( (value < 0) ||
- (fraction < 0) ||
- (value > UINT64_MAX) ||
- (fraction > UINT32_MAX) )
- {
- GNUNET_break_op (0);
- return GNUNET_SYSERR;
- }
- if (strlen (currency) >= TALER_CURRENCY_LEN)
- {
- GNUNET_break_op (0);
- return GNUNET_SYSERR;
- }
- r_amount->value = (uint64_t) value;
- r_amount->fraction = (uint32_t) fraction;
- strcpy (r_amount->currency, currency);
- (void) TALER_amount_normalize (r_amount);
- return GNUNET_OK;
-}
-
-
-/**
- * Parse given JSON object to absolute time.
- *
- * @param json the json object representing Amount
- * @param[out] abs where the amount has to be written
- * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
- */
-int
-TALER_json_to_abs (json_t *json,
- struct GNUNET_TIME_Absolute *abs)
-{
- const char *val;
- unsigned long long int tval;
-
- val = json_string_value (json);
- if (NULL == val)
- {
- GNUNET_break_op (0);
- return GNUNET_SYSERR;
- }
- if ( (0 == strcasecmp (val,
- "/forever/")) ||
- (0 == strcasecmp (val,
- "/never/")) )
- {
- *abs = GNUNET_TIME_UNIT_FOREVER_ABS;
- return GNUNET_OK;
- }
- if (1 != sscanf (val,
- "/Date(%llu)/",
- &tval))
- {
- GNUNET_break_op (0);
- return GNUNET_SYSERR;
- }
- /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */
- abs->abs_value_us = tval * 1000LL * 1000LL;
- if ( (abs->abs_value_us) / 1000LL / 1000LL != tval)
- {
- /* Integer overflow */
- GNUNET_break_op (0);
- return GNUNET_SYSERR;
- }
- return GNUNET_OK;
-}
-
-
-/**
- * Parse given JSON object to data
- *
- * @param json the json object representing data
- * @param out the pointer to hold the parsed data.
- * @param out_size the size of @a out
- * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
- */
-int
-TALER_json_to_data (json_t *json,
- void *out,
- size_t out_size)
-{
- const char *enc;
- unsigned int len;
-
- EXITIF (NULL == (enc = json_string_value (json)));
- len = strlen (enc);
- EXITIF (((len * 5) / 8) != out_size);
- EXITIF (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, len, out, out_size));
- return GNUNET_OK;
- EXITIF_exit:
- return GNUNET_SYSERR;
-}
-
-
-/**
- * Hash a JSON for binary signing.
- *
- * @param[in] json some JSON value
- * @param[out] hc resulting hash code
- * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
- */
-int
-TALER_hash_json (json_t *json,
- struct GNUNET_HashCode *hc)
-{
- char *wire_enc;
- size_t len;
-
- if (NULL == (wire_enc = json_dumps (json,
- JSON_COMPACT | JSON_SORT_KEYS)))
- return GNUNET_SYSERR;
- len = strlen (wire_enc) + 1;
- GNUNET_CRYPTO_hash (wire_enc,
- len,
- hc);
- free (wire_enc);
- return GNUNET_OK;
-}
-
-
-/* End of util/json.c */
diff --git a/src/util/test_json.c b/src/util/test_json.c
deleted file mode 100644
index 5e2f50fe8..000000000
--- a/src/util/test_json.c
+++ /dev/null
@@ -1,182 +0,0 @@
-/*
- This file is part of TALER
- (C) 2015 GNUnet e.V.
-
- TALER is free software; you can redistribute it and/or modify it under the
- terms of the GNU 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 General Public License for more details.
-
- You should have received a copy of the GNU General Public License along with
- TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
-*/
-
-/**
- * @file util/test_json.c
- * @brief Tests for Taler-specific crypto logic
- * @author Christian Grothoff <christian@grothoff.org>
- */
-#include "platform.h"
-#include "taler_util.h"
-#include "taler_json_lib.h"
-
-
-/**
- * Test amount conversion from/to JSON.
- *
- * @return 0 on success
- */
-static int
-test_amount ()
-{
- json_t *j;
- struct TALER_Amount a1;
- struct TALER_Amount a2;
-
- GNUNET_assert (GNUNET_OK ==
- TALER_string_to_amount ("EUR:4.3",
- &a1));
- j = TALER_json_from_amount (&a1);
- GNUNET_assert (NULL != j);
- GNUNET_assert (GNUNET_OK ==
- TALER_json_to_amount (j,
- &a2));
- GNUNET_assert (0 ==
- TALER_amount_cmp (&a1,
- &a2));
- json_decref (j);
- return 0;
-}
-
-
-/**
- * Test time conversion from/to JSON.
- *
- * @return 0 on success
- */
-static int
-test_time ()
-{
- json_t *j;
- struct GNUNET_TIME_Absolute a1;
- struct GNUNET_TIME_Absolute a2;
-
- a1 = GNUNET_TIME_absolute_get ();
- TALER_round_abs_time (&a1);
- j = TALER_json_from_abs (a1);
- GNUNET_assert (NULL != j);
- GNUNET_assert (GNUNET_OK ==
- TALER_json_to_abs (j,
- &a2));
- GNUNET_assert (a1.abs_value_us ==
- a2.abs_value_us);
- json_decref (j);
-
- a1 = GNUNET_TIME_UNIT_FOREVER_ABS;
- j = TALER_json_from_abs (a1);
- GNUNET_assert (NULL != j);
- GNUNET_assert (GNUNET_OK ==
- TALER_json_to_abs (j,
- &a2));
- GNUNET_assert (a1.abs_value_us ==
- a2.abs_value_us);
- json_decref (j);
- return 0;
-}
-
-
-/**
- * Test raw (binary) conversion from/to JSON.
- *
- * @return 0 on success
- */
-static int
-test_raw ()
-{
- char blob[256];
- char blob2[256];
- unsigned int i;
- json_t *j;
-
- for (i=0;i<=256;i++)
- {
- memset (blob, i, i);
- j = TALER_json_from_data (blob, i);
- GNUNET_assert (NULL != j);
- GNUNET_assert (GNUNET_OK ==
- TALER_json_to_data (j,
- blob2,
- i));
- GNUNET_assert (0 ==
- memcmp (blob,
- blob2,
- i));
- }
- return 0;
-}
-
-
-/**
- * Test rsa conversions from/to JSON.
- *
- * @return 0 on success
- */
-static int
-test_rsa ()
-{
- struct GNUNET_CRYPTO_rsa_PublicKey *pub;
- struct GNUNET_CRYPTO_rsa_PublicKey *pub2;
- struct GNUNET_CRYPTO_rsa_Signature *sig;
- struct GNUNET_CRYPTO_rsa_Signature *sig2;
- struct GNUNET_CRYPTO_rsa_PrivateKey *priv;
- char msg[] = "Hello";
- json_t *jp;
- json_t *js;
-
- priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
- pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
- sig = GNUNET_CRYPTO_rsa_sign (priv,
- msg,
- sizeof (msg));
- GNUNET_assert (NULL != (jp = TALER_json_from_rsa_public_key (pub)));
- GNUNET_assert (NULL != (js = TALER_json_from_rsa_signature (sig)));
- GNUNET_assert (NULL != (pub2 = TALER_json_to_rsa_public_key (jp)));
- GNUNET_assert (NULL != (sig2 = TALER_json_to_rsa_signature (js)));
- GNUNET_break (0 ==
- GNUNET_CRYPTO_rsa_signature_cmp (sig,
- sig2));
- GNUNET_break (0 ==
- GNUNET_CRYPTO_rsa_public_key_cmp (pub,
- pub2));
- GNUNET_CRYPTO_rsa_signature_free (sig);
- GNUNET_CRYPTO_rsa_signature_free (sig2);
- GNUNET_CRYPTO_rsa_private_key_free (priv);
- GNUNET_CRYPTO_rsa_public_key_free (pub);
- GNUNET_CRYPTO_rsa_public_key_free (pub2);
- return 0;
-}
-
-
-int
-main(int argc,
- const char *const argv[])
-{
- GNUNET_log_setup ("test-json",
- "WARNING",
- NULL);
- if (0 != test_amount ())
- return 1;
- if (0 != test_time ())
- return 1;
- if (0 != test_raw ())
- return 1;
- if (0 != test_rsa ())
- return 1;
- /* FIXME: test EdDSA signature conversion... */
- return 0;
-}
-
-/* end of test_json.c */
diff --git a/src/util/util.c b/src/util/util.c
index addafacf7..6dcd0dc29 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -56,7 +56,7 @@ TALER_b2s (const void *buf,
GNUNET_free (tmp);
ret[8] = '\0';
return ret;
-}
+}
/**
@@ -90,46 +90,6 @@ TALER_config_get_denom (struct GNUNET_CONFIGURATION_Handle *cfg,
/**
- * Round a time value so that it is suitable for transmission
- * via JSON encodings.
- *
- * @param at time to round
- * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
- * it was just now rounded
- */
-int
-TALER_round_abs_time (struct GNUNET_TIME_Absolute *at)
-{
- if (at->abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
- return GNUNET_OK;
- if (0 == at->abs_value_us % 1000000)
- return GNUNET_OK;
- at->abs_value_us -= at->abs_value_us % 1000000;
- return GNUNET_NO;
-}
-
-
-/**
- * Round a time value so that it is suitable for transmission
- * via JSON encodings.
- *
- * @param rt time to round
- * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
- * it was just now rounded
- */
-int
-TALER_round_rel_time (struct GNUNET_TIME_Relative *rt)
-{
- if (rt->rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
- return GNUNET_OK;
- if (0 == rt->rel_value_us % 1000000)
- return GNUNET_OK;
- rt->rel_value_us -= rt->rel_value_us % 1000000;
- return GNUNET_NO;
-}
-
-
-/**
* Load configuration by parsing all configuration
* files in the given directory.
*
diff --git a/src/util/wireformats.c b/src/util/wireformats.c
deleted file mode 100644
index a69e8a093..000000000
--- a/src/util/wireformats.c
+++ /dev/null
@@ -1,441 +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 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 General Public License for more details.
-
- You should have received a copy of the GNU General Public License along with
- TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
-*/
-/**
- * @file util/wireformats.c
- * @brief helper functions for JSON processing using libjansson
- * @author Sree Harsha Totakura <sreeharsha@totakura.in>
- * @author Christian Grothoff
- */
-#include "platform.h"
-#include <gnunet/gnunet_util_lib.h>
-#include "taler_util.h"
-
-/**
- * Shorthand for exit jumps.
- */
-#define EXITIF(cond) \
- do { \
- if (cond) { GNUNET_break (0); goto EXITIF_exit; } \
- } while (0)
-
-/**
- * Shorthand for JSON parsing related exit jumps.
- */
-#define UNPACK_EXITIF(cond) \
- do { \
- if (cond) { TALER_json_warn (error); goto EXITIF_exit; } \
- } while (0)
-
-
-/* Taken from GNU gettext */
-
-/**
- * Entry in the country table.
- */
-struct table_entry
-{
- /**
- * 2-Character international country code.
- */
- const char *code;
-
- /**
- * Long English name of the country.
- */
- const char *english;
-};
-
-/* Keep the following table in sync with gettext.
- WARNING: the entries should stay sorted according to the code */
-/**
- * List of country codes.
- */
-static const struct table_entry country_table[] =
- {
- { "AE", "U.A.E." },
- { "AF", "Afghanistan" },
- { "AL", "Albania" },
- { "AM", "Armenia" },
- { "AN", "Netherlands Antilles" },
- { "AR", "Argentina" },
- { "AT", "Austria" },
- { "AU", "Australia" },
- { "AZ", "Azerbaijan" },
- { "BA", "Bosnia and Herzegovina" },
- { "BD", "Bangladesh" },
- { "BE", "Belgium" },
- { "BG", "Bulgaria" },
- { "BH", "Bahrain" },
- { "BN", "Brunei Darussalam" },
- { "BO", "Bolivia" },
- { "BR", "Brazil" },
- { "BT", "Bhutan" },
- { "BY", "Belarus" },
- { "BZ", "Belize" },
- { "CA", "Canada" },
- { "CG", "Congo" },
- { "CH", "Switzerland" },
- { "CI", "Cote d'Ivoire" },
- { "CL", "Chile" },
- { "CM", "Cameroon" },
- { "CN", "People's Republic of China" },
- { "CO", "Colombia" },
- { "CR", "Costa Rica" },
- { "CS", "Serbia and Montenegro" },
- { "CZ", "Czech Republic" },
- { "DE", "Germany" },
- { "DK", "Denmark" },
- { "DO", "Dominican Republic" },
- { "DZ", "Algeria" },
- { "EC", "Ecuador" },
- { "EE", "Estonia" },
- { "EG", "Egypt" },
- { "ER", "Eritrea" },
- { "ES", "Spain" },
- { "ET", "Ethiopia" },
- { "FI", "Finland" },
- { "FO", "Faroe Islands" },
- { "FR", "France" },
- { "GB", "United Kingdom" },
- { "GD", "Caribbean" },
- { "GE", "Georgia" },
- { "GL", "Greenland" },
- { "GR", "Greece" },
- { "GT", "Guatemala" },
- { "HK", "Hong Kong" },
- { "HK", "Hong Kong S.A.R." },
- { "HN", "Honduras" },
- { "HR", "Croatia" },
- { "HT", "Haiti" },
- { "HU", "Hungary" },
- { "ID", "Indonesia" },
- { "IE", "Ireland" },
- { "IL", "Israel" },
- { "IN", "India" },
- { "IQ", "Iraq" },
- { "IR", "Iran" },
- { "IS", "Iceland" },
- { "IT", "Italy" },
- { "JM", "Jamaica" },
- { "JO", "Jordan" },
- { "JP", "Japan" },
- { "KE", "Kenya" },
- { "KG", "Kyrgyzstan" },
- { "KH", "Cambodia" },
- { "KR", "South Korea" },
- { "KW", "Kuwait" },
- { "KZ", "Kazakhstan" },
- { "LA", "Laos" },
- { "LB", "Lebanon" },
- { "LI", "Liechtenstein" },
- { "LK", "Sri Lanka" },
- { "LT", "Lithuania" },
- { "LU", "Luxembourg" },
- { "LV", "Latvia" },
- { "LY", "Libya" },
- { "MA", "Morocco" },
- { "MC", "Principality of Monaco" },
- { "MD", "Moldava" },
- { "MD", "Moldova" },
- { "ME", "Montenegro" },
- { "MK", "Former Yugoslav Republic of Macedonia" },
- { "ML", "Mali" },
- { "MM", "Myanmar" },
- { "MN", "Mongolia" },
- { "MO", "Macau S.A.R." },
- { "MT", "Malta" },
- { "MV", "Maldives" },
- { "MX", "Mexico" },
- { "MY", "Malaysia" },
- { "NG", "Nigeria" },
- { "NI", "Nicaragua" },
- { "NL", "Netherlands" },
- { "NO", "Norway" },
- { "NP", "Nepal" },
- { "NZ", "New Zealand" },
- { "OM", "Oman" },
- { "PA", "Panama" },
- { "PE", "Peru" },
- { "PH", "Philippines" },
- { "PK", "Islamic Republic of Pakistan" },
- { "PL", "Poland" },
- { "PR", "Puerto Rico" },
- { "PT", "Portugal" },
- { "PY", "Paraguay" },
- { "QA", "Qatar" },
- { "RE", "Reunion" },
- { "RO", "Romania" },
- { "RS", "Serbia" },
- { "RU", "Russia" },
- { "RW", "Rwanda" },
- { "SA", "Saudi Arabia" },
- { "SE", "Sweden" },
- { "SG", "Singapore" },
- { "SI", "Slovenia" },
- { "SK", "Slovak" },
- { "SN", "Senegal" },
- { "SO", "Somalia" },
- { "SR", "Suriname" },
- { "SV", "El Salvador" },
- { "SY", "Syria" },
- { "TH", "Thailand" },
- { "TJ", "Tajikistan" },
- { "TM", "Turkmenistan" },
- { "TN", "Tunisia" },
- { "TR", "Turkey" },
- { "TT", "Trinidad and Tobago" },
- { "TW", "Taiwan" },
- { "TZ", "Tanzania" },
- { "UA", "Ukraine" },
- { "US", "United States" },
- { "UY", "Uruguay" },
- { "VA", "Vatican" },
- { "VE", "Venezuela" },
- { "VN", "Viet Nam" },
- { "YE", "Yemen" },
- { "ZA", "South Africa" },
- { "ZW", "Zimbabwe" }
- };
-
-
-/**
- * Country code comparator function, for binary search with bsearch().
- *
- * @param ptr1 pointer to a `struct table_entry`
- * @param ptr2 pointer to a `struct table_entry`
- * @return result of strncmp()'ing the 2-digit country codes of the entries
- */
-static int
-cmp_country_code (const void *ptr1,
- const void *ptr2)
-{
- const struct table_entry *cc1 = ptr1;
- const struct table_entry *cc2 = ptr2;
-
- return strncmp (cc1->code, cc2->code, 2);
-}
-
-
-/**
- * Validates given IBAN according to the European Banking Standards. See:
- * http://www.europeanpaymentscouncil.eu/documents/ECBS%20IBAN%20standard%20EBS204_V3.2.pdf
- *
- * @param iban the IBAN number to validate
- * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not
- */
-static int
-validate_iban (const char *iban)
-{
- char cc[2];
- char ibancpy[35];
- struct table_entry cc_entry;
- unsigned int len;
- char *nbuf;
- unsigned int i;
- unsigned int j;
- unsigned long long dividend;
- unsigned long long remainder;
- int nread;
- int ret;
-
- len = strlen (iban);
- if (len > 34)
- return GNUNET_NO;
- strncpy (cc, iban, 2);
- strncpy (ibancpy, iban + 4, len - 4);
- strncpy (ibancpy + len - 4, iban, 4);
- ibancpy[len] = '\0';
- cc_entry.code = cc;
- cc_entry.english = NULL;
- if (NULL ==
- bsearch (&cc_entry,
- country_table,
- sizeof (country_table) / sizeof (struct table_entry),
- sizeof (struct table_entry),
- &cmp_country_code))
- return GNUNET_NO;
- nbuf = GNUNET_malloc ((len * 2) + 1);
- for (i=0, j=0; i < len; i++)
- {
- if (isalpha ((int) ibancpy[i]))
- {
- EXITIF(2 != snprintf(&nbuf[j],
- 3,
- "%2u",
- (ibancpy[i] - 'A' + 10)));
- j += 2;
- continue;
- }
- nbuf[j] = ibancpy[i];
- j++;
- }
- for (j=0;'\0' != nbuf[j];j++)
- GNUNET_assert (isdigit(nbuf[j]));
- GNUNET_assert (sizeof(dividend) >= 8);
- remainder = 0;
- for (i=0; i<j; i+=16)
- {
- EXITIF (1 !=
- (ret = sscanf (&nbuf[i],
- "%16llu %n",
- &dividend,
- &nread)));
- if (0 != remainder)
- dividend += remainder * (pow (10, nread));
- remainder = dividend % 97;
- }
- if (1 == remainder)
- {
- GNUNET_free (nbuf);
- return GNUNET_YES;
- }
- EXITIF_exit:
- GNUNET_free (nbuf);
- return GNUNET_NO;
-}
-
-
-/**
- * Validate SEPA account details.
- *
- * @param wire JSON with the SEPA details
- * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not
- */
-static int
-validate_sepa (const json_t *wire)
-{
- json_error_t error;
- const char *type;
- const char *iban;
- const char *name;
- const char *bic;
- uint64_t r;
- const char *address;
-
- UNPACK_EXITIF (0 != json_unpack_ex
- ((json_t *) wire,
- &error, JSON_STRICT,
- "{"
- "s:s," /* TYPE: sepa */
- "s:s," /* IBAN: iban */
- "s:s," /* name: beneficiary name */
- "s:s," /* BIC: beneficiary bank's BIC */
- "s:i," /* r: random 64-bit integer nounce */
- "s:s" /* address: address of the beneficiary */
- "}",
- "type", &type,
- "IBAN", &iban,
- "name", &name,
- "bic", &bic,
- "r", &r,
- "address", &address));
- if (1 != validate_iban (iban))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "IBAN `%s' invalid\n",
- iban);
- return GNUNET_NO;
- }
- return GNUNET_YES;
- EXITIF_exit:
- return GNUNET_NO;
-}
-
-
-/**
- * Validate TEST account details. The "test" format is used
- * for testing, so this validator does nothing.
- *
- * @param wire JSON with the TEST details
- * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not
- */
-static int
-validate_test (const json_t *wire)
-{
- return GNUNET_YES;
-}
-
-
-/**
- * Handler for a wire format.
- */
-struct FormatHandler
-{
- /**
- * Type handled by this format handler.
- */
- const char *type;
-
- /**
- * Function to call to evaluate the format.
- *
- * @param wire the JSON to evaluate
- * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not
- */
- int (*handler)(const json_t *wire);
-};
-
-
-/**
- * Check if the given wire format JSON object is correctly formatted
- *
- * @param allowed NULL-terminated array of allowed wire format types
- * @param wire the JSON wire format object
- * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not
- */
-int
-TALER_json_validate_wireformat (const char **allowed,
- const json_t *wire)
-{
- static const struct FormatHandler format_handlers[] = {
- { "SEPA", &validate_sepa },
- { "TEST", &validate_test },
- { NULL, NULL}
- };
- unsigned int i;
- const char *stype;
- json_error_t error;
-
- UNPACK_EXITIF (0 != json_unpack_ex
- ((json_t *) wire,
- &error, 0,
- "{s:s}",
- "type", &stype));
- for (i=0;NULL != allowed[i];i++)
- if (0 == strcasecmp (allowed[i],
- stype))
- break;
- if (NULL == allowed[i])
- {
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Wireformat `%s' does not match exchange's allowed formats\n",
- stype);
- return GNUNET_NO;
- }
-
- for (i=0;NULL != format_handlers[i].type;i++)
- if (0 == strcasecmp (format_handlers[i].type,
- stype))
- return format_handlers[i].handler (wire);
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Wireformat `%s' not supported\n",
- stype);
- return GNUNET_NO;
- EXITIF_exit:
- return GNUNET_NO;
-}
-
-/* end of wireformats.c */