buffer lib has moved to GNUnet

This commit is contained in:
Florian Dold 2020-01-15 11:45:53 +01:00
parent 3fa213e41e
commit 4c488a1d0e
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 25 additions and 397 deletions

View File

@ -74,152 +74,6 @@
__LINE__, gcry_strerror (rc)); abort (); } while (0) __LINE__, gcry_strerror (rc)); abort (); } while (0)
/**
* Dynamically growing buffer. Can be used to construct
* strings and other objects with dynamic size.
*
* This structure should, in most cases, be stack-allocated and
* zero-initialized, like:
*
* struct TALER_Buffer my_buffer = { 0 };
*/
struct TALER_Buffer
{
/**
* Capacity of the buffer.
*/
size_t capacity;
/**
* Current write position.
*/
size_t position;
/**
* Backing memory.
*/
char *mem;
/**
* Log a warning if the buffer is grown over its initially allocated capacity.
*/
int warn_grow;
};
/**
* 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);
/**
* 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);
/**
* 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);
/**
* Write a 0-terminated string to a buffer, excluding the 0-terminator.
*
* Grows the buffer if necessary.
*
* @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);
/**
* 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);
/**
* 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, ...);
/**
* 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);
/**
* 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);
/**
* 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);
/** /**
* Initialize Gcrypt library. * Initialize Gcrypt library.
*/ */

View File

@ -42,7 +42,6 @@ libtalerutil_wallet_la_LDFLAGS = \
libtalerutil_la_SOURCES = \ libtalerutil_la_SOURCES = \
amount.c \ amount.c \
buffer.c \
crypto.c \ crypto.c \
crypto_wire.c \ crypto_wire.c \
util.c \ util.c \

View File

@ -1,226 +0,0 @@
/*
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);
}

View File

@ -22,6 +22,7 @@
*/ */
#include "platform.h" #include "platform.h"
#include "gnunet/gnunet_buffer_lib.h"
#include "taler_util.h" #include "taler_util.h"
#include <gcrypt.h> #include <gcrypt.h>
@ -216,14 +217,14 @@ urlencode_len (const char *s)
* @param s string to encode * @param s string to encode
*/ */
static void static void
buffer_write_urlencode (struct TALER_Buffer *buf, const char *s) buffer_write_urlencode (struct GNUNET_Buffer *buf, const char *s)
{ {
TALER_buffer_ensure_remaining (buf, urlencode_len (s) + 1); GNUNET_buffer_ensure_remaining (buf, urlencode_len (s) + 1);
for (size_t i = 0; i < strlen (s); i++) for (size_t i = 0; i < strlen (s); i++)
{ {
if (GNUNET_YES == is_reserved (s[i])) if (GNUNET_YES == is_reserved (s[i]))
TALER_buffer_write_fstr (buf, "%%%02X", s[i]); GNUNET_buffer_write_fstr (buf, "%%%02X", s[i]);
else else
buf->mem[buf->position++] = s[i]; buf->mem[buf->position++] = s[i];
} }
@ -239,10 +240,10 @@ buffer_write_urlencode (struct TALER_Buffer *buf, const char *s)
char * char *
TALER_urlencode (const char *s) TALER_urlencode (const char *s)
{ {
struct TALER_Buffer buf = { 0 }; struct GNUNET_Buffer buf = { 0 };
buffer_write_urlencode (&buf, s); buffer_write_urlencode (&buf, s);
return TALER_buffer_reap_str (&buf); return GNUNET_buffer_reap_str (&buf);
} }
@ -263,7 +264,7 @@ TALER_url_join (const char *base_url,
{ {
unsigned int iparam = 0; unsigned int iparam = 0;
va_list args; va_list args;
struct TALER_Buffer buf = { 0 }; struct GNUNET_Buffer buf = { 0 };
size_t len; size_t len;
GNUNET_assert (NULL != base_url); GNUNET_assert (NULL != base_url);
@ -311,9 +312,9 @@ TALER_url_join (const char *base_url,
} }
va_end (args); va_end (args);
TALER_buffer_prealloc (&buf, len); GNUNET_buffer_prealloc (&buf, len);
TALER_buffer_write_str (&buf, base_url); GNUNET_buffer_write_str (&buf, base_url);
TALER_buffer_write_str (&buf, path); GNUNET_buffer_write_str (&buf, path);
va_start (args, path); va_start (args, path);
while (1) while (1)
@ -327,15 +328,15 @@ TALER_url_join (const char *base_url,
value = va_arg (args, char *); value = va_arg (args, char *);
if (NULL == value) if (NULL == value)
continue; continue;
TALER_buffer_write_str (&buf, (0 == iparam) ? "?" : "&"); GNUNET_buffer_write_str (&buf, (0 == iparam) ? "?" : "&");
iparam++; iparam++;
TALER_buffer_write_str (&buf, key); GNUNET_buffer_write_str (&buf, key);
TALER_buffer_write_str (&buf, "="); GNUNET_buffer_write_str (&buf, "=");
buffer_write_urlencode (&buf, value); buffer_write_urlencode (&buf, value);
} }
va_end (args); va_end (args);
return TALER_buffer_reap_str (&buf); return GNUNET_buffer_reap_str (&buf);
} }
@ -357,7 +358,7 @@ TALER_url_absolute_raw_va (const char *proto,
const char *path, const char *path,
va_list args) va_list args)
{ {
struct TALER_Buffer buf = { 0 }; struct GNUNET_Buffer buf = { 0 };
unsigned int iparam = 0; unsigned int iparam = 0;
size_t len = 0; size_t len = 0;
va_list args2; va_list args2;
@ -380,14 +381,14 @@ TALER_url_absolute_raw_va (const char *proto,
} }
va_end (args2); va_end (args2);
TALER_buffer_prealloc (&buf, len); GNUNET_buffer_prealloc (&buf, len);
TALER_buffer_write_str (&buf, proto); GNUNET_buffer_write_str (&buf, proto);
TALER_buffer_write_str (&buf, "://"); GNUNET_buffer_write_str (&buf, "://");
TALER_buffer_write_str (&buf, host); GNUNET_buffer_write_str (&buf, host);
TALER_buffer_write_path (&buf, prefix); GNUNET_buffer_write_path (&buf, prefix);
TALER_buffer_write_path (&buf, path); GNUNET_buffer_write_path (&buf, path);
va_copy (args2, args); va_copy (args2, args);
while (1) while (1)
@ -400,15 +401,15 @@ TALER_url_absolute_raw_va (const char *proto,
value = va_arg (args, char *); value = va_arg (args, char *);
if (NULL == value) if (NULL == value)
continue; continue;
TALER_buffer_write_str (&buf, (0 == iparam) ? "?" : "&"); GNUNET_buffer_write_str (&buf, (0 == iparam) ? "?" : "&");
iparam++; iparam++;
TALER_buffer_write_str (&buf, key); GNUNET_buffer_write_str (&buf, key);
TALER_buffer_write_str (&buf, "="); GNUNET_buffer_write_str (&buf, "=");
buffer_write_urlencode (&buf, value); buffer_write_urlencode (&buf, value);
} }
va_end (args2); va_end (args2);
return TALER_buffer_reap_str (&buf); return GNUNET_buffer_reap_str (&buf);
} }