exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

commit e4b1cd849f91e082181826321cc584ce38e262af
parent 83a38faa556ad60712dd63e9f6fbc7b119a1aa90
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sat, 21 Feb 2026 21:00:21 +0100

move commonly used helper functions to libtalerutil

Diffstat:
Msrc/exchange/taler-exchange-httpd_aml-accounts-get.c | 54+-----------------------------------------------------
Msrc/exchange/taler-exchange-httpd_batch-deposit.c | 46++--------------------------------------------
Msrc/include/taler/taler_util.h | 20++++++++++++++++++++
Msrc/util/Makefile.am | 3++-
Asrc/util/xml.c | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 125 insertions(+), 98 deletions(-)

diff --git a/src/exchange/taler-exchange-httpd_aml-accounts-get.c b/src/exchange/taler-exchange-httpd_aml-accounts-get.c @@ -126,58 +126,6 @@ free_rc (struct ResponseContext *rc) /** - * Escape @a str for encoding in XML. - * - * @param str string to escape - * @return XML-encoded @a str - */ -static char * -escape_xml (const char *str) -{ - struct GNUNET_Buffer out = { 0 }; - const char *p = str; - - while (*p) - { - const char *esc = NULL; - - switch (*p) - { - case '&': - esc = "&amp;"; - break; - case '<': - esc = "&lt;"; - break; - case '>': - esc = "&gt;"; - break; - case '"': - esc = "&quot;"; - break; - case '\'': - esc = "&apos;"; - break; - } - - if (NULL != esc) - { - GNUNET_buffer_write_str (&out, - esc); - } - else - { - GNUNET_buffer_write (&out, - p, - 1); - } - p++; - } - return GNUNET_buffer_reap_str (&out); -} - - -/** * Return account summary information. * * @param cls closure @@ -250,7 +198,7 @@ record_cb ( (GNUNET_TIME_absolute_is_never (open_time.abs_time)) ) comments = "transacted amounts below limits that trigger account opening"; - ecomments = escape_xml (comments); + ecomments = TALER_escape_xml (comments); tt = (time_t) GNUNET_TIME_timestamp_to_s (open_time); tm = gmtime (&tt); strftime (opentime_s, diff --git a/src/exchange/taler-exchange-httpd_batch-deposit.c b/src/exchange/taler-exchange-httpd_batch-deposit.c @@ -884,49 +884,6 @@ parse_coin (const struct BatchDepositContext *bdc, /** - * We allow [a-zA-Z0-9-.:] in extra_wire_subject_metadata. - * Test @a c for it. - * - * @param c character to test - * @return true if OK - */ -static inline bool -is_allowed_metachar (char c) -{ - return (c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z') || - (c >= '0' && c <= '9') || - c == '-' || - c == '.' || - c == ':'; -} - - -/** - * Check if @a src matches ``[a-zA-Z0-9-.:]{1, 40}`` - * - * @param src string to check - * @return true if it is an allowed metadata string. - */ -static bool -is_valid_meta_string (const char *src) -{ - unsigned int len = 0; - if (NULL == src) - return true; - - while (*src) - { - if (! is_allowed_metachar (*src++)) - return false; - if (++len > 40) - return false; - } - return true; -} - - -/** * Run processing phase that parses the request. * * @param[in,out] bdc request context @@ -999,7 +956,8 @@ bdc_phase_parse (struct BatchDepositContext *bdc, return; } } - if (! is_valid_meta_string (bd->extra_wire_subject_metadata)) + if (! TALER_is_valid_subject_metadata_string ( + bd->extra_wire_subject_metadata)) { GNUNET_break_op (0); GNUNET_JSON_parse_free (spec); diff --git a/src/include/taler/taler_util.h b/src/include/taler/taler_util.h @@ -810,6 +810,26 @@ const char * TALER_yna_to_string (enum TALER_EXCHANGE_YesNoAll yna); +/** + * Escape @a str for encoding in XML. + * + * @param str string to escape + * @return XML-encoded @a str (caller must GNUNET_free()) + */ +char * +TALER_escape_xml (const char *str); + + +/** + * Check if @a src matches ``[a-zA-Z0-9-.:]{1, 40}`` + * + * @param src string to check + * @return true if it is an allowed metadata string. + */ +bool +TALER_is_valid_subject_metadata_string (const char *src); + + #ifdef __APPLE__ /** * Returns the first occurrence of `c` in `s`, or returns the null-byte diff --git a/src/util/Makefile.am b/src/util/Makefile.am @@ -111,6 +111,7 @@ libtalerutil_la_SOURCES = \ url.c \ util.c \ wallet_signatures.c \ + xml.c \ yna.c \ os_installation.c @@ -126,7 +127,7 @@ libtalerutil_la_LIBADD = \ -lm libtalerutil_la_LDFLAGS = \ - -version-info 11:0:1 \ + -version-info 12:0:2 \ -no-undefined diff --git a/src/util/xml.c b/src/util/xml.c @@ -0,0 +1,100 @@ +/* + This file is part of TALER + Copyright (C) 2026 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 + 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, see <http://www.gnu.org/licenses/> +*/ +/** + * @file xml.c + * @brief Common utility functions for XML handling + * @author Christian Grothoff + */ +#include "taler/platform.h" +#include "taler/taler_util.h" + + +/** + * We allow [a-zA-Z0-9-.:] in extra_wire_subject_metadata. + * Test @a c for it. + * + * @param c character to test + * @return true if OK + */ +static inline bool +is_allowed_metachar (char c) +{ + return (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || + c == '-' || + c == '.' || + c == ':'; +} + + +bool +TALER_is_valid_subject_metadata_string (const char *src) +{ + unsigned int len = 0; + if (NULL == src) + return true; + + while (*src) + { + if (! is_allowed_metachar (*src++)) + return false; + if (++len > 40) + return false; + } + return true; +} + + +char * +TALER_escape_xml (const char *str) +{ + struct GNUNET_Buffer out = { 0 }; + const char *p = str; + + while (*p) + { + const char *esc = NULL; + + switch (*p) + { + case '&': + esc = "&amp;"; + break; + case '<': + esc = "&lt;"; + break; + case '>': + esc = "&gt;"; + break; + case '"': + esc = "&quot;"; + break; + case '\'': + esc = "&apos;"; + break; + } + if (NULL != esc) + GNUNET_buffer_write_str (&out, + esc); + else + GNUNET_buffer_write (&out, + p, + 1); + p++; + } + return GNUNET_buffer_reap_str (&out); +}