GNUnet  0.11.x
Functions
buffer.c File Reference

Common buffer management functions. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_buffer_lib.h"
Include dependency graph for buffer.c:

Go to the source code of this file.

Functions

void GNUNET_buffer_prealloc (struct GNUNET_Buffer *buf, size_t capacity)
 Initialize a buffer with the given capacity. More...
 
void GNUNET_buffer_ensure_remaining (struct GNUNET_Buffer *buf, size_t n)
 Make sure that at least n bytes remaining in the buffer. More...
 
void GNUNET_buffer_write (struct GNUNET_Buffer *buf, const char *data, size_t len)
 Write bytes to the buffer. More...
 
void GNUNET_buffer_write_str (struct GNUNET_Buffer *buf, const char *str)
 Write a 0-terminated string to a buffer, excluding the 0-terminator. More...
 
char * GNUNET_buffer_reap_str (struct GNUNET_Buffer *buf)
 Clear the buffer and return the string it contained. More...
 
void * GNUNET_buffer_reap (struct GNUNET_Buffer *buf, size_t *size)
 Clear the buffer and return its contents. More...
 
void GNUNET_buffer_clear (struct GNUNET_Buffer *buf)
 Free the backing memory of the given buffer. More...
 
void GNUNET_buffer_write_path (struct GNUNET_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. More...
 
void GNUNET_buffer_write_fstr (struct GNUNET_Buffer *buf, const char *fmt,...)
 Write a 0-terminated formatted string to a buffer, excluding the 0-terminator. More...
 
void GNUNET_buffer_write_vfstr (struct GNUNET_Buffer *buf, const char *fmt, va_list args)
 Write a 0-terminated formatted string to a buffer, excluding the 0-terminator. More...
 
void GNUNET_buffer_write_data_encoded (struct GNUNET_Buffer *buf, const void *data, size_t data_len)
 Write data encoded via GNUNET_STRINGS_data_to_string to the buffer. More...
 

Detailed Description

Common buffer management functions.

Author
Florian Dold

Definition in file buffer.c.

Function Documentation

◆ GNUNET_buffer_prealloc()

void GNUNET_buffer_prealloc ( struct GNUNET_Buffer buf,
size_t  capacity 
)

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.

Parameters
bufthe buffer to initialize
capacitythe capacity (in bytes) to allocate for buf

Definition at line 35 of file buffer.c.

References GNUNET_Buffer::capacity, GNUNET_assert, GNUNET_malloc, GNUNET_YES, GNUNET_Buffer::mem, GNUNET_Buffer::position, and GNUNET_Buffer::warn_grow.

37 {
38  /* Buffer should be zero-initialized */
39  GNUNET_assert (0 == buf->mem);
40  GNUNET_assert (0 == buf->capacity);
41  GNUNET_assert (0 == buf->position);
42  buf->mem = GNUNET_malloc (capacity);
43  buf->capacity = capacity;
44  buf->warn_grow = GNUNET_YES;
45 }
size_t capacity
Capacity of the buffer.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
char * mem
Backing memory.
size_t position
Current write position.
int warn_grow
Log a warning if the buffer is grown over its initially allocated capacity.
#define GNUNET_malloc(size)
Wrapper around malloc.

◆ GNUNET_buffer_ensure_remaining()

void GNUNET_buffer_ensure_remaining ( struct GNUNET_Buffer buf,
size_t  n 
)

Make sure that at least n bytes remaining in the buffer.

Parameters
bufbuffer to potentially grow
nnumber of bytes that should be available to write

Definition at line 55 of file buffer.c.

References GNUNET_Buffer::capacity, GNUNET_assert, GNUNET_break, GNUNET_malloc, GNUNET_realloc, GNUNET_YES, GNUNET_Buffer::mem, GNUNET_Buffer::position, and GNUNET_Buffer::warn_grow.

Referenced by GNUNET_buffer_reap_str(), GNUNET_buffer_write(), GNUNET_buffer_write_data_encoded(), GNUNET_buffer_write_path(), and GNUNET_buffer_write_vfstr().

57 {
58  size_t new_capacity = buf->position + n;
59 
60  /* guard against overflow */
61  GNUNET_assert (new_capacity >= buf->position);
62  if (new_capacity <= buf->capacity)
63  return;
64  /* warn if calculation of expected size was wrong */
66  if (new_capacity < buf->capacity * 2)
67  new_capacity = buf->capacity * 2;
68  buf->capacity = new_capacity;
69  if (NULL != buf->mem)
70  buf->mem = GNUNET_realloc (buf->mem, new_capacity);
71  else
72  buf->mem = GNUNET_malloc (new_capacity);
73 }
size_t capacity
Capacity of the buffer.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
char * mem
Backing memory.
size_t position
Current write position.
int warn_grow
Log a warning if the buffer is grown over its initially allocated capacity.
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the caller graph for this function:

◆ GNUNET_buffer_write()

void GNUNET_buffer_write ( struct GNUNET_Buffer buf,
const char *  data,
size_t  len 
)

Write bytes to the buffer.

Grows the buffer if necessary.

Parameters
bufbuffer to write to
datadata to read from
lennumber of bytes to copy from data to buf

Definition at line 86 of file buffer.c.

References GNUNET_buffer_ensure_remaining(), len, GNUNET_Buffer::mem, and GNUNET_Buffer::position.

Referenced by GNUNET_buffer_write_path(), GNUNET_buffer_write_str(), GNUNET_STRINGS_urlencode(), and write_to_buffer().

89 {
91  memcpy (buf->mem + buf->position, data, len);
92  buf->position += len;
93 }
char * mem
Backing memory.
size_t position
Current write position.
void GNUNET_buffer_ensure_remaining(struct GNUNET_Buffer *buf, size_t n)
Make sure that at least n bytes remaining in the buffer.
Definition: buffer.c:55
uint32_t data
The data value.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_buffer_write_str()

void GNUNET_buffer_write_str ( struct GNUNET_Buffer buf,
const char *  str 
)

Write a 0-terminated string to a buffer, excluding the 0-terminator.

Parameters
bufthe buffer to write to
strthe string to write to buf

Definition at line 103 of file buffer.c.

References GNUNET_buffer_write(), and len.

Referenced by login_redirect().

105 {
106  size_t len = strlen (str);
107 
108  GNUNET_buffer_write (buf, str, len);
109 }
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition: buffer.c:86
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_buffer_reap_str()

char* GNUNET_buffer_reap_str ( struct GNUNET_Buffer buf)

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.

Parameters
bufthe buffer to reap the string from
Returns
the buffer contained in the string

Definition at line 123 of file buffer.c.

References GNUNET_buffer_ensure_remaining(), GNUNET_Buffer::mem, GNUNET_Buffer::position, and res.

Referenced by GNUNET_STRINGS_urlencode(), and login_redirect().

124 {
125  char *res;
126 
127  /* ensure 0-termination */
128  if ( (0 == buf->position) || ('\0' != buf->mem[buf->position - 1]))
129  {
131  buf->mem[buf->position++] = '\0';
132  }
133  res = buf->mem;
134  memset (buf, 0, sizeof (struct GNUNET_Buffer));
135  return res;
136 }
char * mem
Backing memory.
size_t position
Current write position.
static int res
void GNUNET_buffer_ensure_remaining(struct GNUNET_Buffer *buf, size_t n)
Make sure that at least n bytes remaining in the buffer.
Definition: buffer.c:55
Common buffer management functions.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_buffer_reap()

void* GNUNET_buffer_reap ( struct GNUNET_Buffer buf,
size_t *  size 
)

Clear the buffer and return its contents.

The caller is responsible to eventually GNUNET_free the returned data.

Parameters
bufthe buffer to reap the contents from
sizewhere to store the size of the returned data
Returns
the data contained in the string

Definition at line 149 of file buffer.c.

References GNUNET_Buffer::mem, GNUNET_Buffer::position, and res.

Referenced by GNUNET_BIO_get_buffer_contents().

150 {
151  *size = buf->position;
152  void *res = buf->mem;
153  memset (buf, 0, sizeof (struct GNUNET_Buffer));
154  return res;
155 }
char * mem
Backing memory.
size_t position
Current write position.
static int res
static unsigned int size
Size of the "table".
Definition: peer.c:67
Common buffer management functions.
Here is the caller graph for this function:

◆ GNUNET_buffer_clear()

void GNUNET_buffer_clear ( struct GNUNET_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.

Definition at line 164 of file buffer.c.

References GNUNET_free, and GNUNET_Buffer::mem.

Referenced by GNUNET_BIO_write_close(), and GNUNET_STRINGS_urlencode().

165 {
166  GNUNET_free (buf->mem);
167  memset (buf, 0, sizeof (struct GNUNET_Buffer));
168 }
char * mem
Backing memory.
Common buffer management functions.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ GNUNET_buffer_write_path()

void GNUNET_buffer_write_path ( struct GNUNET_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.

Parameters
bufbuffer to write to
strstring containing the new path component

Definition at line 180 of file buffer.c.

References GNUNET_buffer_ensure_remaining(), GNUNET_buffer_write(), len, GNUNET_Buffer::mem, and GNUNET_Buffer::position.

181 {
182  size_t len = strlen (str);
183 
184  while ( (0 != len) && ('/' == str[0]) )
185  {
186  str++;
187  len--;
188  }
189  if ( (0 == buf->position) || ('/' != buf->mem[buf->position - 1]) )
190  {
192  buf->mem[buf->position++] = '/';
193  }
194  GNUNET_buffer_write (buf, str, len);
195 }
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition: buffer.c:86
char * mem
Backing memory.
size_t position
Current write position.
void GNUNET_buffer_ensure_remaining(struct GNUNET_Buffer *buf, size_t n)
Make sure that at least n bytes remaining in the buffer.
Definition: buffer.c:55
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:

◆ GNUNET_buffer_write_fstr()

void GNUNET_buffer_write_fstr ( struct GNUNET_Buffer buf,
const char *  fmt,
  ... 
)

Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.

Grows the buffer if necessary.

Parameters
bufthe buffer to write to
fmtformat string
...format arguments

Definition at line 209 of file buffer.c.

References gnunet-chk::args, and GNUNET_buffer_write_vfstr().

Referenced by GNUNET_STRINGS_urlencode(), and login_redirect().

210 {
211  va_list args;
212 
213  va_start (args, fmt);
214  GNUNET_buffer_write_vfstr (buf, fmt, args);
215  va_end (args);
216 }
void GNUNET_buffer_write_vfstr(struct GNUNET_Buffer *buf, const char *fmt, va_list args)
Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.
Definition: buffer.c:230
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_buffer_write_vfstr()

void GNUNET_buffer_write_vfstr ( struct GNUNET_Buffer buf,
const char *  fmt,
va_list  args 
)

Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.

Grows the buffer if necessary.

Parameters
bufthe buffer to write to
fmtformat string
argsformat argument list

Definition at line 230 of file buffer.c.

References GNUNET_Buffer::capacity, GNUNET_assert, GNUNET_buffer_ensure_remaining(), GNUNET_Buffer::mem, GNUNET_Buffer::position, and res.

Referenced by GNUNET_buffer_write_fstr().

233 {
234  int res;
235  va_list args2;
236 
237  va_copy (args2, args);
238  res = vsnprintf (NULL, 0, fmt, args2);
239  va_end (args2);
240 
241  GNUNET_assert (res >= 0);
242  GNUNET_buffer_ensure_remaining (buf, res + 1);
243 
244  va_copy (args2, args);
245  res = vsnprintf (buf->mem + buf->position, res + 1, fmt, args2);
246  va_end (args2);
247 
248  GNUNET_assert (res >= 0);
249  buf->position += res;
250  GNUNET_assert (buf->position <= buf->capacity);
251 }
size_t capacity
Capacity of the buffer.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
char * mem
Backing memory.
size_t position
Current write position.
static int res
void GNUNET_buffer_ensure_remaining(struct GNUNET_Buffer *buf, size_t n)
Make sure that at least n bytes remaining in the buffer.
Definition: buffer.c:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_buffer_write_data_encoded()

void GNUNET_buffer_write_data_encoded ( struct GNUNET_Buffer buf,
const void *  data,
size_t  data_len 
)

Write data encoded via GNUNET_STRINGS_data_to_string to the buffer.

Grows the buffer if necessary.

Parameters
bufbuffer to write to
datadata to read from
data_lennumber of bytes to copy from data to buf

Definition at line 264 of file buffer.c.

References GNUNET_Buffer::capacity, GNUNET_assert, GNUNET_buffer_ensure_remaining(), GNUNET_STRINGS_data_to_string(), GNUNET_Buffer::mem, and GNUNET_Buffer::position.

267 {
268  size_t outlen = data_len * 8;
269 
270  if (outlen % 5 > 0)
271  outlen += 5 - outlen % 5;
272  outlen /= 5;
274  outlen);
275  GNUNET_assert (NULL !=
277  data_len,
278  (buf->mem
279  + buf->position),
280  outlen));
281  buf->position += outlen;
282  GNUNET_assert (buf->position <= buf->capacity);
283 }
size_t capacity
Capacity of the buffer.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
char * mem
Backing memory.
size_t position
Current write position.
void GNUNET_buffer_ensure_remaining(struct GNUNET_Buffer *buf, size_t n)
Make sure that at least n bytes remaining in the buffer.
Definition: buffer.c:55
uint32_t data
The data value.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:871
Here is the call graph for this function: