From 868a4ce76d543aaf0de7a12dbbddd3fd1d4571b6 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 23 Oct 2021 07:11:23 +0200 Subject: -resovling more FTBFS issues --- src/pq/pq_query_helper.c | 262 +++++++++++++++++++++++++++++++++++--------- src/pq/pq_result_helper.c | 270 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 436 insertions(+), 96 deletions(-) (limited to 'src/pq') diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c index f558cbccf..3f51ddbe8 100644 --- a/src/pq/pq_query_helper.c +++ b/src/pq/pq_query_helper.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2014, 2015, 2016 Taler Systems SA + Copyright (C) 2014, 2015, 2016, 2021 Taler Systems SA 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 @@ -70,20 +70,16 @@ qconv_amount_nbo (void *cls, } -/** - * Generate query parameter for a currency, consisting of the three - * components "value", "fraction" and "currency" in this order. The - * types must be a 64-bit integer, 32-bit integer and a - * #TALER_CURRENCY_LEN-sized BLOB/VARCHAR respectively. - * - * @param x pointer to the query parameter to pass - * @return array entry for the query parameters to use - */ struct GNUNET_PQ_QueryParam TALER_PQ_query_param_amount_nbo (const struct TALER_AmountNBO *x) { - struct GNUNET_PQ_QueryParam res = - { &qconv_amount_nbo, NULL, x, sizeof (*x), 2 }; + struct GNUNET_PQ_QueryParam res = { + .conv = &qconv_amount_nbo, + .data = x, + .size = sizeof (*x), + .num_params = 2 + }; + return res; } @@ -138,20 +134,193 @@ qconv_amount (void *cls, } +struct GNUNET_PQ_QueryParam +TALER_PQ_query_param_amount (const struct TALER_Amount *x) +{ + struct GNUNET_PQ_QueryParam res = { + .conv = &qconv_amount, + .data = x, + .size = sizeof (*x), + .num_params = 2 + }; + + return res; +} + + /** - * Generate query parameter for a currency, consisting of the three - * components "value", "fraction" and "currency" in this order. The - * types must be a 64-bit integer, 32-bit integer and a - * #TALER_CURRENCY_LEN-sized BLOB/VARCHAR respectively. + * Function called to convert input argument into SQL parameters. * - * @param x pointer to the query parameter to pass - * @return array entry for the query parameters to use + * @param cls closure + * @param data pointer to input argument + * @param data_len number of bytes in @a data (if applicable) + * @param[out] param_values SQL data to set + * @param[out] param_lengths SQL length data to set + * @param[out] param_formats SQL format data to set + * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays + * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc() + * @param scratch_length number of entries left in @a scratch + * @return -1 on error, number of offsets used in @a scratch otherwise */ +static int +qconv_denom_pub (void *cls, + const void *data, + size_t data_len, + void *param_values[], + int param_lengths[], + int param_formats[], + unsigned int param_length, + void *scratch[], + unsigned int scratch_length) +{ + const struct TALER_DenominationPublicKey *denom_pub = data; + size_t tlen; + size_t len; + uint32_t be[2]; + char *buf; + void *tbuf; + + (void) cls; + GNUNET_assert (1 == param_length); + GNUNET_assert (scratch_length > 0); + GNUNET_break (NULL == cls); + be[0] = htonl ((uint32_t) denom_pub->cipher); + be[1] = htonl (denom_pub->age_mask); + switch (denom_pub->cipher) + { + case TALER_DENOMINATION_RSA: + tlen = GNUNET_CRYPTO_rsa_public_key_encode ( + denom_pub->details.rsa_public_key, + &tbuf); + break; + // TODO: add case for Clause-Schnorr + default: + GNUNET_assert (0); + } + len = tlen + sizeof (be); + buf = GNUNET_malloc (len); + memcpy (buf, + be, + sizeof (be)); + switch (denom_pub->cipher) + { + case TALER_DENOMINATION_RSA: + memcpy (&buf[sizeof (be)], + tbuf, + tlen); + GNUNET_free (tbuf); + break; + // TODO: add case for Clause-Schnorr + default: + GNUNET_assert (0); + } + + scratch[0] = buf; + param_values[0] = (void *) buf; + param_lengths[0] = len; + param_formats[0] = 1; + return 1; +} + + struct GNUNET_PQ_QueryParam -TALER_PQ_query_param_amount (const struct TALER_Amount *x) +TALER_PQ_query_param_denom_pub ( + const struct TALER_DenominationPublicKey *denom_pub) +{ + struct GNUNET_PQ_QueryParam res = { + .conv = &qconv_denom_pub, + .data = denom_pub, + .num_params = 1 + }; + + return res; +} + + +/** + * Function called to convert input argument into SQL parameters. + * + * @param cls closure + * @param data pointer to input argument + * @param data_len number of bytes in @a data (if applicable) + * @param[out] param_values SQL data to set + * @param[out] param_lengths SQL length data to set + * @param[out] param_formats SQL format data to set + * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays + * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc() + * @param scratch_length number of entries left in @a scratch + * @return -1 on error, number of offsets used in @a scratch otherwise + */ +static int +qconv_denom_sig (void *cls, + const void *data, + size_t data_len, + void *param_values[], + int param_lengths[], + int param_formats[], + unsigned int param_length, + void *scratch[], + unsigned int scratch_length) +{ + const struct TALER_DenominationSignature *denom_sig = data; + size_t tlen; + size_t len; + uint32_t be; + char *buf; + void *tbuf; + + (void) cls; + GNUNET_assert (1 == param_length); + GNUNET_assert (scratch_length > 0); + GNUNET_break (NULL == cls); + be = htonl ((uint32_t) denom_sig->cipher); + switch (denom_sig->cipher) + { + case TALER_DENOMINATION_RSA: + tlen = GNUNET_CRYPTO_rsa_signature_encode ( + denom_sig->details.rsa_signature, + &tbuf); + break; + // TODO: add case for Clause-Schnorr + default: + GNUNET_assert (0); + } + len = tlen + sizeof (be); + buf = GNUNET_malloc (len); + memcpy (buf, + &be, + sizeof (be)); + switch (denom_sig->cipher) + { + case TALER_DENOMINATION_RSA: + memcpy (&buf[sizeof (be)], + tbuf, + tlen); + GNUNET_free (tbuf); + break; + // TODO: add case for Clause-Schnorr + default: + GNUNET_assert (0); + } + + scratch[0] = buf; + param_values[0] = (void *) buf; + param_lengths[0] = len; + param_formats[0] = 1; + return 1; +} + + +struct GNUNET_PQ_QueryParam +TALER_PQ_query_param_denom_sig ( + const struct TALER_DenominationSignature *denom_sig) { - struct GNUNET_PQ_QueryParam res = - { &qconv_amount, NULL, x, sizeof (*x), 2 }; + struct GNUNET_PQ_QueryParam res = { + .conv = &qconv_denom_sig, + .data = denom_sig, + .num_params = 1 + }; + return res; } @@ -199,17 +368,15 @@ qconv_json (void *cls, } -/** - * Generate query parameter for a JSON object (stored as a string - * in the DB). - * - * @param x pointer to the json object to pass - */ struct GNUNET_PQ_QueryParam TALER_PQ_query_param_json (const json_t *x) { - struct GNUNET_PQ_QueryParam res = - { &qconv_json, NULL, x, 0, 1 }; + struct GNUNET_PQ_QueryParam res = { + .conv = &qconv_json, + .data = x, + .num_params = 1 + }; + return res; } @@ -261,21 +428,16 @@ qconv_round_time (void *cls, } -/** - * Generate query parameter for an absolute time value. - * In contrast to - * #GNUNET_PQ_query_param_absolute_time(), this function - * will abort (!) if the time given is not rounded! - * The database must store a 64-bit integer. - * - * @param x pointer to the query parameter to pass - * @return array entry for the query parameters to use - */ struct GNUNET_PQ_QueryParam TALER_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x) { - struct GNUNET_PQ_QueryParam res = - { &qconv_round_time, NULL, x, sizeof (*x), 1 }; + struct GNUNET_PQ_QueryParam res = { + .conv = &qconv_round_time, + .data = x, + .size = sizeof (*x), + .num_params = 1 + }; + return res; } @@ -324,20 +486,16 @@ qconv_round_time_abs (void *cls, } -/** - * Generate query parameter for an absolute time value. - * In contrast to - * #GNUNET_PQ_query_param_absolute_time(), this function - * will abort (!) if the time given is not rounded! - * The database must store a 64-bit integer. - * - * @param x pointer to the query parameter to pass - */ struct GNUNET_PQ_QueryParam TALER_PQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x) { - struct GNUNET_PQ_QueryParam res = - { &qconv_round_time_abs, NULL, x, sizeof (*x), 1 }; + struct GNUNET_PQ_QueryParam res = { + .conv = &qconv_round_time_abs, + .data = x, + .size = sizeof (*x), + .num_params = 1 + }; + return res; } diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c index 3d252890d..57bcf6dd3 100644 --- a/src/pq/pq_result_helper.c +++ b/src/pq/pq_result_helper.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2014, 2015, 2016 Taler Systems SA + Copyright (C) 2014, 2015, 2016, 2021 Taler Systems SA 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 @@ -156,14 +156,6 @@ extract_amount_nbo (void *cls, } -/** - * Currency amount expected. - * - * @param name name of the field in the table - * @param currency the currency to use for @a amount - * @param[out] amount where to store the result - * @return array entry for the result specification to use - */ struct GNUNET_PQ_ResultSpec TALER_PQ_result_spec_amount_nbo (const char *name, const char *currency, @@ -240,14 +232,6 @@ extract_amount (void *cls, } -/** - * Currency amount expected. - * - * @param name name of the field in the table - * @param currency the currency to use for @a amount - * @param[out] amount where to store the result - * @return array entry for the result specification to use - */ struct GNUNET_PQ_ResultSpec TALER_PQ_result_spec_amount (const char *name, const char *currency, @@ -353,13 +337,6 @@ clean_json (void *cls, } -/** - * json_t expected. - * - * @param name name of the field in the table - * @param[out] jp where to store the result - * @return array entry for the result specification to use - */ struct GNUNET_PQ_ResultSpec TALER_PQ_result_spec_json (const char *name, json_t **jp) @@ -430,16 +407,6 @@ extract_round_time (void *cls, } -/** - * Rounded absolute time expected. - * In contrast to #GNUNET_PQ_query_param_absolute_time_nbo(), - * this function ensures that the result is rounded and can - * be converted to JSON. - * - * @param name name of the field in the table - * @param[out] at where to store the result - * @return array entry for the result specification to use - */ struct GNUNET_PQ_ResultSpec TALER_PQ_result_spec_absolute_time (const char *name, struct GNUNET_TIME_Absolute *at) @@ -510,16 +477,6 @@ extract_round_time_nbo (void *cls, } -/** - * Rounded absolute time in network byte order expected. - * In contrast to #GNUNET_PQ_query_param_absolute_time_nbo(), - * this function ensures that the result is rounded and can - * be converted to JSON. - * - * @param name name of the field in the table - * @param[out] at where to store the result - * @return array entry for the result specification to use - */ struct GNUNET_PQ_ResultSpec TALER_PQ_result_spec_absolute_time_nbo (const char *name, struct GNUNET_TIME_AbsoluteNBO *at) @@ -535,4 +492,229 @@ TALER_PQ_result_spec_absolute_time_nbo (const char *name, } +/** + * Extract data from a Postgres database @a result at row @a row. + * + * @param cls closure + * @param result where to extract data from + * @param int row to extract data from + * @param fname name (or prefix) of the fields to extract from + * @param[in,out] dst_size where to store size of result, may be NULL + * @param[out] dst where to store the result + * @return + * #GNUNET_YES if all results could be extracted + * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) + */ +static enum GNUNET_GenericReturnValue +extract_denom_pub (void *cls, + PGresult *result, + int row, + const char *fname, + size_t *dst_size, + void *dst) +{ + struct TALER_DenominationPublicKey *pk = dst; + size_t len; + const char *res; + int fnum; + uint32_t be[2]; + + (void) cls; + fnum = PQfnumber (result, + fname); + if (fnum < 0) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + if (PQgetisnull (result, + row, + fnum)) + return GNUNET_NO; + + /* if a field is null, continue but + * remember that we now return a different result */ + len = PQgetlength (result, + row, + fnum); + res = PQgetvalue (result, + row, + fnum); + if (len < sizeof (be)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + memcpy (be, + res, + sizeof (be)); + res += sizeof (be); + len -= sizeof (be); + pk->cipher = ntohl (be[0]); + pk->age_mask = ntohl (be[1]); + switch (pk->cipher) + { + case TALER_DENOMINATION_RSA: + pk->details.rsa_public_key + = GNUNET_CRYPTO_rsa_public_key_decode (res, + len); + if (NULL == pk->details.rsa_public_key) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + return GNUNET_OK; + // FIXME: add CS case! + default: + GNUNET_break (0); + } + return GNUNET_SYSERR; +} + + +/** + * Function called to clean up memory allocated + * by a #GNUNET_PQ_ResultConverter. + * + * @param cls closure + * @param rd result data to clean up + */ +static void +clean_denom_pub (void *cls, + void *rd) +{ + struct TALER_DenominationPublicKey *denom_pub = rd; + + (void) cls; + TALER_denom_pub_free (denom_pub); +} + + +struct GNUNET_PQ_ResultSpec +TALER_PQ_result_spec_denom_pub (const char *name, + struct TALER_DenominationPublicKey *denom_pub) +{ + struct GNUNET_PQ_ResultSpec res = { + .conv = &extract_denom_pub, + .cleaner = &clean_denom_pub, + .dst = (void *) denom_pub, + .fname = name + }; + + return res; +} + + +/** + * Extract data from a Postgres database @a result at row @a row. + * + * @param cls closure + * @param result where to extract data from + * @param int row to extract data from + * @param fname name (or prefix) of the fields to extract from + * @param[in,out] dst_size where to store size of result, may be NULL + * @param[out] dst where to store the result + * @return + * #GNUNET_YES if all results could be extracted + * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) + */ +static enum GNUNET_GenericReturnValue +extract_denom_sig (void *cls, + PGresult *result, + int row, + const char *fname, + size_t *dst_size, + void *dst) +{ + struct TALER_DenominationSignature *sig = dst; + size_t len; + const char *res; + int fnum; + uint32_t be; + + (void) cls; + fnum = PQfnumber (result, + fname); + if (fnum < 0) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + if (PQgetisnull (result, + row, + fnum)) + return GNUNET_NO; + + /* if a field is null, continue but + * remember that we now return a different result */ + len = PQgetlength (result, + row, + fnum); + res = PQgetvalue (result, + row, + fnum); + if (len < sizeof (be)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + memcpy (&be, + res, + sizeof (be)); + res += sizeof (be); + len -= sizeof (be); + sig->cipher = ntohl (be); + switch (sig->cipher) + { + case TALER_DENOMINATION_RSA: + sig->details.rsa_signature + = GNUNET_CRYPTO_rsa_signature_decode (res, + len); + if (NULL == sig->details.rsa_signature) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + return GNUNET_OK; + // FIXME: add CS case! + default: + GNUNET_break (0); + } + return GNUNET_SYSERR; +} + + +/** + * Function called to clean up memory allocated + * by a #GNUNET_PQ_ResultConverter. + * + * @param cls closure + * @param rd result data to clean up + */ +static void +clean_denom_sig (void *cls, + void *rd) +{ + struct TALER_DenominationSignature *denom_sig = rd; + + (void) cls; + TALER_denom_sig_free (denom_sig); +} + + +struct GNUNET_PQ_ResultSpec +TALER_PQ_result_spec_denom_sig (const char *name, + struct TALER_DenominationSignature *denom_sig) +{ + struct GNUNET_PQ_ResultSpec res = { + .conv = &extract_denom_sig, + .cleaner = &clean_denom_sig, + .dst = (void *) denom_sig, + .fname = name + }; + + return res; +} + + /* end of pq_result_helper.c */ -- cgit v1.2.3