1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
/* $Id: buffer.c 7585 2006-11-21 09:37:51Z eagle $
**
** Counted, reusable memory buffer.
**
** A buffer is an allocated bit of memory with a known size and a separate
** data length. It's intended to store strings and can be reused repeatedly
** to minimize the number of memory allocations. Buffers increase in
** increments of 1K.
**
** A buffer contains a notion of the data that's been used and the data
** that's been left, used when the buffer is an I/O buffer where lots of data
** is buffered and then slowly processed out of the buffer. The total length
** of the data is used + left. If a buffer is just used to store some data,
** used can be set to 0 and left stores the length of the data.
*/
#include "config.h"
#include "clibrary.h"
#include <errno.h>
#include <sys/stat.h>
#include "inn/buffer.h"
#include "inn/libinn.h"
/*
** Allocate a new struct buffer and initialize it.
*/
struct buffer *
buffer_new(void)
{
struct buffer *buffer;
buffer = xmalloc(sizeof(struct buffer));
buffer->size = 0;
buffer->used = 0;
buffer->left = 0;
buffer->data = NULL;
return buffer;
}
/*
** Free a buffer.
*/
void
buffer_free(struct buffer *buffer)
{
if (buffer->data != NULL)
free(buffer->data);
free(buffer);
}
/*
** Resize a buffer to be at least as large as the provided second argument.
** Resize buffers to multiples of 1KB to keep the number of reallocations to
** a minimum. Refuse to resize a buffer to make it smaller.
*/
void
buffer_resize(struct buffer *buffer, size_t size)
{
if (size < buffer->size)
return;
buffer->size = (size + 1023) & ~1023UL;
buffer->data = xrealloc(buffer->data, buffer->size);
}
/*
** Compact a buffer by moving the data between buffer->used and buffer->left
** to the beginning of the buffer, overwriting the already-consumed data.
*/
void
buffer_compact(struct buffer *buffer)
{
if (buffer->used == 0)
return;
if (buffer->left != 0)
memmove(buffer->data, buffer->data + buffer->used, buffer->left);
buffer->used = 0;
}
/*
** Replace whatever data is currently in the buffer with the provided data.
** Resize the buffer if needed.
*/
void
buffer_set(struct buffer *buffer, const char *data, size_t length)
{
if (length > 0) {
buffer_resize(buffer, length);
memmove(buffer->data, data, length);
}
buffer->left = length;
buffer->used = 0;
}
/*
** Append data to a buffer. The new data shows up as additional unused data
** at the end of the buffer. Resize the buffer if needed.
*/
void
buffer_append(struct buffer *buffer, const char *data, size_t length)
{
size_t total;
if (length == 0)
return;
total = buffer->used + buffer->left;
buffer_resize(buffer, total + length);
buffer->left += length;
memcpy(buffer->data + total, data, length);
}
/*
** Print data into a buffer from the supplied va_list, either appending to
** the end of it or replacing the existing contents. The new data shows up
** as unused data at the end of the buffer. The trailing nul is not added to
** the buffer.
*/
void
buffer_vsprintf(struct buffer *buffer, bool append, const char *format,
va_list args)
{
size_t total, avail;
ssize_t status;
va_list args_copy;
if (!append)
buffer_set(buffer, NULL, 0);
total = buffer->used + buffer->left;
avail = buffer->size - total;
va_copy(args_copy, args);
status = vsnprintf(buffer->data + total, avail, format, args_copy);
va_end(args_copy);
if (status < 0)
return;
if ((size_t) status + 1 <= avail) {
buffer->left += status;
} else {
buffer_resize(buffer, total + status + 1);
avail = buffer->size - total;
status = vsnprintf(buffer->data + total, avail, format, args);
if (status < 0 || (size_t) status + 1 > avail)
return;
buffer->left += status;
}
}
/*
** Print data into a buffer, either appending to the end of it or replacing
** the existing contents. The new data shows up as unused data at the end of
** the buffer. Resize the buffer if needed. The trailing nul is not added
** to the buffer.
*/
void
buffer_sprintf(struct buffer *buffer, bool append, const char *format, ...)
{
va_list args;
va_start(args, format);
buffer_vsprintf(buffer, append, format, args);
va_end(args);
}
/*
** Swap the contents of two buffers.
*/
void
buffer_swap(struct buffer *one, struct buffer *two)
{
struct buffer tmp;
tmp = *one;
*one = *two;
*two = tmp;
}
/*
** Find a given string in the unconsumed data in buffer. We know that all
** the data prior to start (an offset into the space between buffer->used and
** buffer->left) has already been searched. Returns the offset of the string
** (with the same meaning as start) in offset if found, and returns true if
** the terminator is found and false otherwise.
*/
bool
buffer_find_string(struct buffer *buffer, const char *string, size_t start,
size_t *offset)
{
char *terminator, *data;
size_t length;
length = strlen(string);
do {
data = buffer->data + buffer->used + start;
terminator = memchr(data, string[0], buffer->left - start);
if (terminator == NULL)
return false;
start = (terminator - buffer->data) - buffer->used;
if (buffer->left - start < length)
return false;
start++;
} while (memcmp(terminator, string, length) != 0);
*offset = start - 1;
return true;
}
/*
** Read from a file descriptor into a buffer, up to the available space in
** the buffer, and return the number of characters read.
*/
ssize_t
buffer_read(struct buffer *buffer, int fd)
{
ssize_t count;
do {
size_t used = buffer->used + buffer->left;
count = read(fd, buffer->data + used, buffer->size - used);
} while (count == -1 && (errno == EAGAIN || errno == EINTR));
if (count > 0)
buffer->left += count;
return count;
}
/*
** Read from a file descriptor until end of file is reached, doubling the
** buffer size as necessary to hold all of the data. Returns true on
** success, false on failure (in which case errno will be set).
*/
bool
buffer_read_all(struct buffer *buffer, int fd)
{
ssize_t count;
if (buffer->size == 0)
buffer_resize(buffer, 1024);
do {
size_t used = buffer->used + buffer->left;
if (buffer->size <= used)
buffer_resize(buffer, buffer->size * 2);
count = buffer_read(buffer, fd);
} while (count > 0);
return (count == 0);
}
/*
** Read the entire contents of a file into a buffer. This is a slight
** optimization over buffer_read_all because it can stat the file descriptor
** first and size the buffer appropriately. buffer_read_all will still
** handle the case where the file size changes while it's being read.
** Returns true on success, false on failure (in which case errno will be
** set).
*/
bool
buffer_read_file(struct buffer *buffer, int fd)
{
struct stat st;
size_t used = buffer->used + buffer->left;
if (fstat(fd, &st) < 0)
return false;
buffer_resize(buffer, st.st_size + used);
return buffer_read_all(buffer, fd);
}
|