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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/*
* dynbuf.c
*
* Dynamic allocated buffers.
*
* Copyright (C) 2004,2005 Martin Schlemmer <azarah@nosferatu.za.org>
*
*
* This program 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 version 2 of the License.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Header$
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rcscripts.h"
static dyn_buf_t *reallocate_dyn_buf (dyn_buf_t *dynbuf, size_t needed);
dyn_buf_t *
new_dyn_buf (void)
{
dyn_buf_t *dynbuf = NULL;
dynbuf = xmalloc (sizeof (dyn_buf_t));
if (NULL == dynbuf)
return NULL;
dynbuf->data = xmalloc (DYNAMIC_BUFFER_SIZE);
if (NULL == dynbuf->data)
{
free (dynbuf);
return NULL;
}
dynbuf->length = DYNAMIC_BUFFER_SIZE;
dynbuf->rd_index = 0;
dynbuf->wr_index = 0;
dynbuf->file_map = FALSE;
return dynbuf;
}
dyn_buf_t *
new_dyn_buf_mmap_file (const char *name)
{
dyn_buf_t *dynbuf = NULL;
dynbuf = xmalloc (sizeof (dyn_buf_t));
if (NULL == dynbuf)
return NULL;
if (-1 == file_map (name, &dynbuf->data, &dynbuf->length))
{
DBG_MSG ("Failed to mmap file '%s'\n", name);
free (dynbuf);
return NULL;
}
dynbuf->wr_index = dynbuf->length;
dynbuf->rd_index = 0;
dynbuf->file_map = TRUE;
return dynbuf;
}
dyn_buf_t *
reallocate_dyn_buf (dyn_buf_t *dynbuf, size_t needed)
{
int len;
if (!check_arg_dyn_buf (dynbuf))
return NULL;
if (dynbuf->file_map)
{
errno = EPERM;
DBG_MSG ("Cannot reallocate mmap()'d file!\n");
return NULL;
}
len = sizeof (char) * (dynbuf->wr_index + needed + 1);
if (dynbuf->length < len)
{
char *new_ptr;
/* Increase size in chunks to minimize reallocations */
if (len < (dynbuf->length + DYNAMIC_BUFFER_SIZE))
len = dynbuf->length + DYNAMIC_BUFFER_SIZE;
new_ptr = xrealloc (dynbuf->data, len);
if (NULL == new_ptr)
return NULL;
dynbuf->data = new_ptr;
dynbuf->length = len;
}
return dynbuf;
}
void
free_dyn_buf (dyn_buf_t *dynbuf)
{
if (NULL == dynbuf)
return;
if (!dynbuf->file_map)
{
if (NULL != dynbuf->data)
{
free (dynbuf->data);
dynbuf->data = NULL;
}
}
else
{
save_errno ();
file_unmap (dynbuf->data, dynbuf->length);
restore_errno ();
}
dynbuf->length = 0;
dynbuf->rd_index = 0;
dynbuf->wr_index = 0;
free (dynbuf);
dynbuf = NULL;
}
int
write_dyn_buf (dyn_buf_t *dynbuf, const char *buf, size_t length)
{
int len;
if (!check_arg_dyn_buf (dynbuf))
return -1;
if (!check_arg_str (buf))
return -1;
if (dynbuf->file_map)
{
errno = EPERM;
DBG_MSG ("Cannot write to readonly mmap()'d file!\n");
return -1;
}
if (NULL == reallocate_dyn_buf (dynbuf, length))
{
DBG_MSG ("Could not reallocate dynamic buffer!\n");
return -1;
}
len = snprintf ((dynbuf->data + dynbuf->wr_index), length + 1, "%s", buf);
/* If len is less than length, it means the string was shorter than
* given length */
if (length > len)
length = len;
if (0 < length)
dynbuf->wr_index += length;
if (-1 == length)
DBG_MSG ("Failed to write to dynamic buffer!\n");
return length;
}
int write_dyn_buf_from_fd (int fd, dyn_buf_t *dynbuf, size_t length)
{
int len = length;
if (!check_arg_dyn_buf (dynbuf))
return -1;
if (!check_arg_fd (fd))
return -1;
if (dynbuf->file_map)
{
errno = EPERM;
DBG_MSG ("Cannot write to readonly mmap()'d file!\n");
return -1;
}
if (NULL == reallocate_dyn_buf (dynbuf, length))
{
DBG_MSG ("Could not reallocate dynamic buffer!\n");
return -1;
}
len = read (fd, (dynbuf->data + dynbuf->wr_index), len);
if (length > len)
length = len;
if (0 < length)
dynbuf->wr_index += length;
dynbuf->data[dynbuf->wr_index] = '\0';
if (-1 == length)
DBG_MSG ("Failed to write to dynamic buffer!\n");
return length;
}
int
sprintf_dyn_buf (dyn_buf_t *dynbuf, const char *format, ...)
{
va_list arg1, arg2;
char test_str[10];
int needed, written = 0;
if (!check_arg_dyn_buf (dynbuf))
return -1;
if (!check_arg_str (format))
return -1;
if (dynbuf->file_map)
{
errno = EPERM;
DBG_MSG ("Cannot write to readonly mmap()'d file!\n");
return -1;
}
va_start (arg1, format);
va_copy (arg2, arg1);
/* XXX: Lame way to try and figure out how much space we need */
needed = vsnprintf (test_str, sizeof (test_str), format, arg2);
va_end (arg2);
if (NULL == reallocate_dyn_buf (dynbuf, needed))
{
DBG_MSG ("Could not reallocate dynamic buffer!\n");
return -1;
}
written = vsnprintf ((dynbuf->data + dynbuf->wr_index), needed + 1,
format, arg1);
va_end (arg1);
if (0 < written)
dynbuf->wr_index += written;
if (-1 == written)
DBG_MSG ("Failed to write to dynamic buffer!\n");
return written;
}
int
read_dyn_buf (dyn_buf_t *dynbuf, char *buf, size_t length)
{
int len = length;
if (!check_arg_dyn_buf (dynbuf))
return -1;
if (!check_arg_ptr (buf))
return -1;
if (dynbuf->rd_index >= dynbuf->length)
return 0;
if (dynbuf->wr_index < (dynbuf->rd_index + length))
len = dynbuf->wr_index - dynbuf->rd_index;
len = snprintf (buf, len + 1, "%s", (dynbuf->data + dynbuf->rd_index));
/* If len is less than length, it means the string was shorter than
* given length */
if (length > len)
length = len;
if (0 < length)
dynbuf->rd_index += length;
if (-1 == length)
DBG_MSG ("Failed to write from dynamic buffer!\n");
return length;
}
int
read_dyn_buf_to_fd (int fd, dyn_buf_t *dynbuf, size_t length)
{
int len = length;
if (!check_arg_dyn_buf (dynbuf))
return -1;
if (!check_arg_fd (fd))
return -1;
if (dynbuf->rd_index >= dynbuf->length)
return 0;
if (dynbuf->wr_index < (dynbuf->rd_index + length))
len = dynbuf->wr_index - dynbuf->rd_index;
len = write (fd, (dynbuf->data + dynbuf->rd_index), len);
if (length > len)
length = len;
if (0 < length)
dynbuf->rd_index += length;
if (-1 == length)
DBG_MSG ("Failed to write from dynamic buffer!\n");
return length;
}
char *
read_line_dyn_buf (dyn_buf_t *dynbuf)
{
char *buf = NULL;
size_t count = 0;
if (!check_arg_dyn_buf (dynbuf))
return NULL;
if (dynbuf->rd_index == dynbuf->wr_index)
return NULL;
for (count = dynbuf->rd_index; count < dynbuf->wr_index && dynbuf->data[count] != '\n'; count++);
if (count <= dynbuf->wr_index)
{
buf = xstrndup ((dynbuf->data + dynbuf->rd_index),
(count - dynbuf->rd_index));
if (NULL == buf)
return NULL;
dynbuf->rd_index = count;
/* Also skip the '\n' .. */
if (dynbuf->rd_index < dynbuf->wr_index)
dynbuf->rd_index++;
}
return buf;
}
bool
dyn_buf_rd_eof (dyn_buf_t *dynbuf)
{
if (!check_arg_dyn_buf (dynbuf))
return FALSE;
if (dynbuf->rd_index >= dynbuf->wr_index)
return TRUE;
return FALSE;
}
inline bool
check_dyn_buf (dyn_buf_t *dynbuf)
{
if ((NULL == dynbuf) || (NULL == dynbuf->data) || (0 == dynbuf->length))
return FALSE;
return TRUE;
}
inline bool
__check_arg_dyn_buf (dyn_buf_t *dynbuf, const char *file, const char *func,
size_t line)
{
if (!check_dyn_buf (dynbuf))
{
errno = EINVAL;
debug_message (file, func, line, "Invalid dynamic buffer passed!\n");
return FALSE;
}
return TRUE;
}
|