3 A bare-bones string stream.
5 Copyright (C) 2002 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
45 nettle_buffer_grow(struct nettle_buffer *buffer,
48 assert(buffer->size <= buffer->alloc);
50 if (buffer->size + length > buffer->alloc)
58 alloc = buffer->alloc * 2 + length + 100;
59 p = buffer->realloc(buffer->realloc_ctx, buffer->contents, alloc);
64 buffer->alloc = alloc;
70 nettle_buffer_init_realloc(struct nettle_buffer *buffer,
72 nettle_realloc_func *realloc)
74 buffer->contents = NULL;
76 buffer->realloc = realloc;
77 buffer->realloc_ctx = realloc_ctx;
82 nettle_buffer_init_size(struct nettle_buffer *buffer,
83 size_t length, uint8_t *space)
85 buffer->contents = space;
86 buffer->alloc = length;
87 buffer->realloc = NULL;
88 buffer->realloc_ctx = NULL;
93 nettle_buffer_clear(struct nettle_buffer *buffer)
96 buffer->realloc(buffer->realloc_ctx, buffer->contents, 0);
98 buffer->contents = NULL;
104 nettle_buffer_reset(struct nettle_buffer *buffer)
110 nettle_buffer_space(struct nettle_buffer *buffer,
115 if (!nettle_buffer_grow(buffer, length))
118 p = buffer->contents + buffer->size;
119 buffer->size += length;
124 nettle_buffer_write(struct nettle_buffer *buffer,
125 size_t length, const uint8_t *data)
127 uint8_t *p = nettle_buffer_space(buffer, length);
130 memcpy(p, data, length);
138 nettle_buffer_copy(struct nettle_buffer *dst,
139 const struct nettle_buffer *src)
141 return nettle_buffer_write(dst, src->size, src->contents);