-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.c
311 lines (241 loc) · 5.12 KB
/
buffer.c
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/***************************************************************************
begin........: April 2015
copyright....: Sebastian Fedrau
email........: [email protected]
***************************************************************************/
/***************************************************************************
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
This program 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 v3 for more details.
***************************************************************************/
/**
* \file buffer.c
* \brief Byte buffer.
* \author Sebastian Fedrau <[email protected]>
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <assert.h>
#include "buffer.h"
/*! @cond INTERNAL */
#define RETURN_IF_INVALID(b) if(!buffer_is_valid(b)) return
#define RETURN_VAL_IF_INVALID(b, v) if(!buffer_is_valid(b)) return v
#define BUFFER_LIMIT (SSIZE_MAX - 1)
#define EXCEEDS_BUFFER_MAX_SIZE(buf, len) buf->max_size - buf->len < len
/*! @endcond */
Buffer *
buffer_new(size_t max_size)
{
assert(max_size > 0 && max_size <= BUFFER_LIMIT);
Buffer *buf = (Buffer *)malloc(sizeof(Buffer));
if(!buf)
{
perror("malloc()");
abort();
}
buffer_init(buf, max_size);
return buf;
}
void
buffer_init(Buffer *buf, size_t max_size)
{
assert(buf != NULL);
assert(max_size > 0 && max_size <= BUFFER_LIMIT);
memset(buf, 0, sizeof(Buffer));
buf->max_size = max_size;
buf->msize = 64;
buf->valid = true;
buf->data = (char *)malloc(buf->msize);
if(!buf->data)
{
perror("malloc()");
abort();
}
}
void
buffer_free(Buffer *buf)
{
assert(buf != NULL);
free(buf->data);
}
void
buffer_destroy(Buffer *buf)
{
assert(buf != NULL);
buffer_free(buf);
free(buf);
}
void
buffer_clear(Buffer *buf)
{
assert(buf != NULL);
buf->len = 0;
buf->valid = true;
}
size_t
buffer_len(const Buffer *buf)
{
assert(buf != NULL);
RETURN_VAL_IF_INVALID(buf, 0);
return buf->len;
}
bool
buffer_is_valid(const Buffer *buf)
{
assert(buf != NULL);
return buf->valid;
}
bool
buffer_is_empty(const Buffer *buf)
{
assert(buf != NULL);
RETURN_VAL_IF_INVALID(buf, false);
return buf->len == 0;
}
static size_t
_buffer_new_realloc_size(size_t from, size_t to)
{
assert(from < to);
size_t size = from;
while(size < to)
{
size *= 2;
if(size < from)
{
size = to;
}
}
return size;
}
bool
buffer_fill(Buffer *buf, const char *data, size_t len)
{
assert(buf != NULL);
assert(data != NULL);
RETURN_VAL_IF_INVALID(buf, false);
if(EXCEEDS_BUFFER_MAX_SIZE(buf, len))
{
fprintf(stderr, "%s(): buffer exceeds allowed maximum size.\n", __func__);
buf->valid = false;
}
else
{
if(len > buf->msize - buf->len)
{
size_t new_size = _buffer_new_realloc_size(buf->msize, buf->msize + len);
buf->msize = new_size;
buf->data = (char *)realloc(buf->data, new_size);
if(!buf->data)
{
perror("realloc()");
abort();
}
}
memcpy(buf->data + buf->len, data, len);
buf->len += len;
}
return buf->valid;
}
ssize_t
buffer_fill_from_fd(Buffer *buf, int fd, size_t count)
{
assert(buf != NULL);
assert(fd >= 0);
assert(count <= buf->max_size);
RETURN_VAL_IF_INVALID(buf, 0);
ssize_t bytes;
char data[count];
if((bytes = read(fd, data, count)) > 0)
{
if(!buffer_fill(buf, data, bytes))
{
bytes = -1;
}
}
else if(bytes == -1)
{
perror("read()");
}
return bytes;
}
static void
_buffer_copy_to_string(const Buffer *buf, size_t count, char **dst, size_t *len)
{
assert(buf != NULL);
assert(dst != NULL);
assert(len != NULL);
assert(count <= buf->len);
if(count + 1 > *len)
{
*len = count;
*dst = (char *)realloc(*dst, count + 1);
if(!*dst)
{
perror("realloc()");
abort();
}
}
memcpy(*dst, buf->data, count);
(*dst)[count] = '\0';
}
bool
buffer_read_line(Buffer *buf, char **dst, size_t *len)
{
assert(buf != NULL);
assert(dst != NULL);
assert(len != NULL);
RETURN_VAL_IF_INVALID(buf, false);
char *ptr;
bool found = false;
if((ptr = memchr(buf->data, '\n', buf->len)))
{
size_t slen = ptr - buf->data;
_buffer_copy_to_string(buf, slen, dst, len);
buf->len -= slen + 1;
memmove(buf->data, ptr + 1, buf->len);
found = true;
}
return found;
}
bool
buffer_flush(const Buffer *buf, char **dst, size_t *len)
{
assert(buf != NULL);
assert(dst != NULL);
assert(len != NULL);
RETURN_VAL_IF_INVALID(buf, false);
bool flushed = false;
if(buf->len)
{
_buffer_copy_to_string(buf, buf->len, dst, len);
flushed = true;
}
return flushed;
}
char *
buffer_to_string(const Buffer *buf)
{
assert(buf != NULL);
RETURN_VAL_IF_INVALID(buf, NULL);
char *str = NULL;
if(buf->len)
{
str = (char *)malloc(buf->len + 1);
if(!str)
{
perror("malloc()");
abort();
}
memcpy(str, buf->data, buf->len);
str[buf->len] = '\0';
}
return str;
}