diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/Makefile.am | 1 | ||||
-rw-r--r-- | src/util/buffer.c | 226 | ||||
-rw-r--r-- | src/util/util.c | 207 |
3 files changed, 228 insertions, 206 deletions
diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 35abe4a1b..3e76a6ba5 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -42,6 +42,7 @@ libtalerutil_wallet_la_LDFLAGS = \ libtalerutil_la_SOURCES = \ amount.c \ + buffer.c \ crypto.c \ crypto_wire.c \ util.c \ diff --git a/src/util/buffer.c b/src/util/buffer.c new file mode 100644 index 000000000..85762322a --- /dev/null +++ b/src/util/buffer.c @@ -0,0 +1,226 @@ +/* + This file is part of TALER + Copyright (C) 2019 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, see <http://www.gnu.org/licenses/> +*/ +/** + * @file util.c + * @brief Common buffer management functions; we might choose to move those to GNUnet at some point + * @author Florian Dold + */ +#include "platform.h" +#include "taler_util.h" +#include <gcrypt.h> + +/** + * Initialize a buffer with the given capacity. + * + * When a buffer is allocated with this function, a warning is logged + * when the buffer exceeds the initial capacity. + * + * @param buf the buffer to initialize + * @param capacity the capacity (in bytes) to allocate for @a buf + */ +void +TALER_buffer_prealloc (struct TALER_Buffer *buf, size_t capacity) +{ + /* Buffer should be zero-initialized */ + GNUNET_assert (0 == buf->mem); + GNUNET_assert (0 == buf->capacity); + GNUNET_assert (0 == buf->position); + buf->mem = GNUNET_malloc (capacity); + buf->capacity = capacity; + buf->warn_grow = GNUNET_YES; +} + + +/** + * Make sure that at least @a n bytes remaining in the buffer. + * + * @param buf buffer to potentially grow + * @param n number of bytes that should be available to write + */ +void +TALER_buffer_ensure_remaining (struct TALER_Buffer *buf, size_t n) +{ + size_t new_capacity = buf->position + n; + + if (new_capacity <= buf->capacity) + return; + /* warn if calculation of expected size was wrong */ + GNUNET_break (GNUNET_YES != buf->warn_grow); + if (new_capacity < buf->capacity * 2) + new_capacity = buf->capacity * 2; + buf->capacity = new_capacity; + if (NULL != buf->mem) + buf->mem = GNUNET_realloc (buf->mem, new_capacity); + else + buf->mem = GNUNET_malloc (new_capacity); +} + + +/** + * Write bytes to the buffer. + * + * Grows the buffer if necessary. + * + * @param buf buffer to write to + * @param data data to read from + * @param len number of bytes to copy from @a data to @a buf + * + */ +void +TALER_buffer_write (struct TALER_Buffer *buf, const char *data, size_t len) +{ + TALER_buffer_ensure_remaining (buf, len); + memcpy (buf->mem + buf->position, data, len); + buf->position += len; +} + + +/** + * Write a 0-terminated string to a buffer, excluding the 0-terminator. + * + * @param buf the buffer to write to + * @param str the string to write to @a buf + */ +void +TALER_buffer_write_str (struct TALER_Buffer *buf, const char *str) +{ + size_t len = strlen (str); + + TALER_buffer_write (buf, str, len); +} + + +/** + * Clear the buffer and return the string it contained. + * The caller is responsible to eventually #GNUNET_free + * the returned string. + * + * The returned string is always 0-terminated. + * + * @param buf the buffer to reap the string from + * @returns the buffer contained in the string + */ +char * +TALER_buffer_reap_str (struct TALER_Buffer *buf) +{ + char *res; + + /* ensure 0-termination */ + if ( (0 == buf->position) || ('\0' != buf->mem[buf->position - 1])) + { + TALER_buffer_ensure_remaining (buf, 1); + buf->mem[buf->position++] = '\0'; + } + res = buf->mem; + *buf = (struct TALER_Buffer) { 0 }; + return res; +} + + +/** + * Free the backing memory of the given buffer. + * Does not free the memory of the buffer control structure, + * which is typically stack-allocated. + */ +void +TALER_buffer_clear (struct TALER_Buffer *buf) +{ + GNUNET_free_non_null (buf->mem); + *buf = (struct TALER_Buffer) { 0 }; +} + + +/** + * Write a path component to a buffer, ensuring that + * there is exactly one slash between the previous contents + * of the buffer and the new string. + * + * @param buf buffer to write to + * @param str string containing the new path component + */ +void +TALER_buffer_write_path (struct TALER_Buffer *buf, const char *str) +{ + size_t len = strlen (str); + + while ( (0 != len) && ('/' == str[0]) ) + { + str++; + len--; + } + if ( (0 == buf->position) || ('/' != buf->mem[buf->position - 1]) ) + { + TALER_buffer_ensure_remaining (buf, 1); + buf->mem[buf->position++] = '/'; + } + TALER_buffer_write (buf, str, len); +} + + +/** + * Write a 0-terminated formatted string to a buffer, excluding the + * 0-terminator. + * + * Grows the buffer if necessary. + * + * @param buf the buffer to write to + * @param fmt format string + * @param ... format arguments + */ +void +TALER_buffer_write_fstr (struct TALER_Buffer *buf, const char *fmt, ...) +{ + va_list args; + + va_start (args, fmt); + TALER_buffer_write_vfstr (buf, fmt, args); + va_end (args); +} + + +/** + * Write a 0-terminated formatted string to a buffer, excluding the + * 0-terminator. + * + * Grows the buffer if necessary. + * + * @param buf the buffer to write to + * @param fmt format string + * @param args format argument list + */ +void +TALER_buffer_write_vfstr (struct TALER_Buffer *buf, + const char *fmt, + va_list args) +{ + int res; + va_list args2; + + va_copy (args2, args); + res = vsnprintf (NULL, 0, fmt, args2); + va_end (args2); + + GNUNET_assert (res >= 0); + TALER_buffer_ensure_remaining (buf, res + 1); + + va_copy (args2, args); + res = vsnprintf (buf->mem + buf->position, res + 1, fmt, args2); + va_end (args2); + + GNUNET_assert (res >= 0); + buf->position += res; + GNUNET_assert (buf->position <= buf->capacity); +} diff --git a/src/util/util.c b/src/util/util.c index 5a06a1229..42c3b7e73 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2014 GNUnet e.V. + Copyright (C) 2014-2020 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 @@ -13,7 +13,6 @@ 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 util.c * @brief Common utility functions; we might choose to move those to GNUnet at some point @@ -541,210 +540,6 @@ TALER_url_absolute_mhd (struct MHD_Connection *connection, /** - * Initialize a buffer with the given capacity. - * - * When a buffer is allocated with this function, a warning is logged - * when the buffer exceeds the initial capacity. - * - * @param buf the buffer to initialize - * @param capacity the capacity (in bytes) to allocate for @a buf - */ -void -TALER_buffer_prealloc (struct TALER_Buffer *buf, size_t capacity) -{ - /* Buffer should be zero-initialized */ - GNUNET_assert (0 == buf->mem); - GNUNET_assert (0 == buf->capacity); - GNUNET_assert (0 == buf->position); - buf->mem = GNUNET_malloc (capacity); - buf->capacity = capacity; - buf->warn_grow = GNUNET_YES; -} - - -/** - * Make sure that at least @a n bytes remaining in the buffer. - * - * @param buf buffer to potentially grow - * @param n number of bytes that should be available to write - */ -void -TALER_buffer_ensure_remaining (struct TALER_Buffer *buf, size_t n) -{ - size_t new_capacity = buf->position + n; - - if (new_capacity <= buf->capacity) - return; - /* warn if calculation of expected size was wrong */ - GNUNET_break (GNUNET_YES != buf->warn_grow); - if (new_capacity < buf->capacity * 2) - new_capacity = buf->capacity * 2; - buf->capacity = new_capacity; - if (NULL != buf->mem) - buf->mem = GNUNET_realloc (buf->mem, new_capacity); - else - buf->mem = GNUNET_malloc (new_capacity); -} - - -/** - * Write bytes to the buffer. - * - * Grows the buffer if necessary. - * - * @param buf buffer to write to - * @param data data to read from - * @param len number of bytes to copy from @a data to @a buf - * - */ -void -TALER_buffer_write (struct TALER_Buffer *buf, const char *data, size_t len) -{ - TALER_buffer_ensure_remaining (buf, len); - memcpy (buf->mem + buf->position, data, len); - buf->position += len; -} - - -/** - * Write a 0-terminated string to a buffer, excluding the 0-terminator. - * - * @param buf the buffer to write to - * @param str the string to write to @a buf - */ -void -TALER_buffer_write_str (struct TALER_Buffer *buf, const char *str) -{ - size_t len = strlen (str); - - TALER_buffer_write (buf, str, len); -} - - -/** - * Clear the buffer and return the string it contained. - * The caller is responsible to eventually #GNUNET_free - * the returned string. - * - * The returned string is always 0-terminated. - * - * @param buf the buffer to reap the string from - * @returns the buffer contained in the string - */ -char * -TALER_buffer_reap_str (struct TALER_Buffer *buf) -{ - char *res; - - /* ensure 0-termination */ - if ( (0 == buf->position) || ('\0' != buf->mem[buf->position - 1])) - { - TALER_buffer_ensure_remaining (buf, 1); - buf->mem[buf->position++] = '\0'; - } - res = buf->mem; - *buf = (struct TALER_Buffer) { 0 }; - return res; -} - - -/** - * Free the backing memory of the given buffer. - * Does not free the memory of the buffer control structure, - * which is typically stack-allocated. - */ -void -TALER_buffer_clear (struct TALER_Buffer *buf) -{ - GNUNET_free_non_null (buf->mem); - *buf = (struct TALER_Buffer) { 0 }; -} - - -/** - * Write a path component to a buffer, ensuring that - * there is exactly one slash between the previous contents - * of the buffer and the new string. - * - * @param buf buffer to write to - * @param str string containing the new path component - */ -void -TALER_buffer_write_path (struct TALER_Buffer *buf, const char *str) -{ - size_t len = strlen (str); - - while ( (0 != len) && ('/' == str[0]) ) - { - str++; - len--; - } - if ( (0 == buf->position) || ('/' != buf->mem[buf->position - 1]) ) - { - TALER_buffer_ensure_remaining (buf, 1); - buf->mem[buf->position++] = '/'; - } - TALER_buffer_write (buf, str, len); -} - - -/** - * Write a 0-terminated formatted string to a buffer, excluding the - * 0-terminator. - * - * Grows the buffer if necessary. - * - * @param buf the buffer to write to - * @param fmt format string - * @param ... format arguments - */ -void -TALER_buffer_write_fstr (struct TALER_Buffer *buf, const char *fmt, ...) -{ - va_list args; - - va_start (args, fmt); - TALER_buffer_write_vfstr (buf, fmt, args); - va_end (args); -} - - -/** - * Write a 0-terminated formatted string to a buffer, excluding the - * 0-terminator. - * - * Grows the buffer if necessary. - * - * @param buf the buffer to write to - * @param fmt format string - * @param args format argument list - */ -void -TALER_buffer_write_vfstr (struct TALER_Buffer *buf, - const char *fmt, - va_list args) -{ - int res; - va_list args2; - - va_copy (args2, args); - res = vsnprintf (NULL, 0, fmt, args2); - va_end (args2); - - GNUNET_assert (res >= 0); - TALER_buffer_ensure_remaining (buf, res + 1); - - va_copy (args2, args); - res = vsnprintf (buf->mem + buf->position, res + 1, fmt, args2); - va_end (args2); - - GNUNET_assert (res >= 0); - buf->position += res; - GNUNET_assert (buf->position <= buf->capacity); -} - - -/** * Prefix of PAYTO URLs. */ #define PAYTO "payto://" |