From b2e6fcae1a9b96f086c61f13f4c2c98338c4e414 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 7 Feb 2022 13:14:25 +0100 Subject: fix DB API for generic blinded planchet storage --- src/pq/pq_result_helper.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) (limited to 'src/pq/pq_result_helper.c') diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c index 02733f298..6ee5da53e 100644 --- a/src/pq/pq_result_helper.c +++ b/src/pq/pq_result_helper.c @@ -730,4 +730,129 @@ TALER_PQ_result_spec_blinded_denom_sig ( } +/** + * 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_blinded_planchet (void *cls, + PGresult *result, + int row, + const char *fname, + size_t *dst_size, + void *dst) +{ + struct TALER_BlindedPlanchet *bp = dst; + size_t len; + const char *res; + int fnum; + uint32_t be[2]; + + (void) cls; + (void) dst_size; + 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)); + if (0x0100 != ntohl (be[1])) /* magic marker: blinded */ + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + res += sizeof (be); + len -= sizeof (be); + bp->cipher = ntohl (be[0]); + switch (bp->cipher) + { + case TALER_DENOMINATION_RSA: + bp->details.rsa_blinded_planchet.blinded_msg_size + = len; + bp->details.rsa_blinded_planchet.blinded_msg + = GNUNET_memdup (res, + len); + return GNUNET_OK; + case TALER_DENOMINATION_CS: + if (sizeof (bp->details.cs_blinded_planchet) != len) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + memcpy (&bp->details.cs_blinded_planchet, + res, + len); + return GNUNET_OK; + 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_blinded_planchet (void *cls, + void *rd) +{ + struct TALER_BlindedPlanchet *bp = rd; + + (void) cls; + TALER_blinded_planchet_free (bp); +} + + +struct GNUNET_PQ_ResultSpec +TALER_PQ_result_spec_blinded_planchet ( + const char *name, + struct TALER_BlindedPlanchet *bp) +{ + struct GNUNET_PQ_ResultSpec res = { + .conv = &extract_blinded_planchet, + .cleaner = &clean_blinded_planchet, + .dst = (void *) bp, + .fname = name + }; + + return res; +} + + /* end of pq_result_helper.c */ -- cgit v1.2.3