-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbuf.c
495 lines (372 loc) · 10.2 KB
/
nbuf.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
TRURLib dynamic buffer
Copyright (C) 2000 Pawel A. Gajda ([email protected])
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef USE_N_ASSERT
# include "nassert.h"
#else
# include <assert.h>
# define n_assert(expr) assert(expr)
#endif
#include "trurl_internal.h"
#include "nmalloc.h"
#include "nbuf.h"
#include "n_snprintf.h"
#include "nstream.h"
#include "nstore.h"
#include "ndie.h"
tn_buf *n_buf_new(int initial_size)
{
tn_buf *buf;
if ((buf = n_calloc(1, sizeof(*buf))) == NULL)
return NULL;
if (initial_size == 0) {
buf->data = NULL;
return buf;
}
buf->data = NULL;
buf->allocated = initial_size;
buf->data = n_malloc(initial_size);
buf->data[0] = '\0';
return buf;
}
tn_buf *n_buf_init(tn_buf *buf, void *buffer, int size)
{
n_assert(buf->data == NULL);
buf->off = 0;
buf->data = buffer;
buf->allocated = size;
buf->size = size;
buf->flags = TN_BUF_CONSTSIZE | TN_BUF_CONSTDATA;
return buf;
}
tn_buf *n_buf_ctl(tn_buf *buf, unsigned flags)
{
buf->flags |= flags;
return buf;
}
tn_buf *n_buf_clean(tn_buf *buf)
{
if (buf->flags & TN_BUF_CONSTDATA) {
buf->data = NULL;
buf->allocated = 0;
buf->flags = 0;
buf->off = 0;
} else {
buf->size = 0;
buf->off = 0;
buf->data[0] = '\0';
}
return buf;
}
void n_buf_free(tn_buf *buf)
{
if (buf->_refcnt > 0) {
buf->_refcnt--;
return;
}
if (!(buf->flags & TN_BUF_CONSTDATA))
free(buf->data);
buf->data = NULL;
free(buf);
}
static tn_buf *n_buf_realloc(tn_buf *buf, size_t new_size)
{
register int diff;
diff = new_size - buf->allocated;
n_assert(diff > 0);
if (buf->flags & TN_BUF_CONSTSIZE) {
trurl_die("n_buf_grow: grow request for const size buffer");
return NULL;
}
if (diff > 0) {
void *tmp;
DBGF("%p to %d: allocated %d, size %d, off %d\n", new_size,
buf, buf->allocated, buf->size, buf->off);
if ((tmp = n_realloc(buf->data, new_size)) == NULL) {
return NULL;
} else {
buf->data = tmp;
buf->allocated = new_size;
}
}
return buf;
}
static tn_buf *n_buf_grow(tn_buf *buf, size_t req_size)
{
register size_t new_size = buf->allocated;
if (new_size == 0)
new_size = 2;
while (req_size >= new_size)
new_size *= 2;
return n_buf_realloc(buf, new_size);
}
#if 0
int n_buf_size(const tn_buf *buf)
{
return buf->size;
}
void *n_buf_ptr(const tn_buf *buf)
{
return buf->data;
}
#endif
int n_buf_seek(tn_buf *buf, int off, int whence)
{
switch (whence) {
case SEEK_CUR:
buf->off += off;
break;
case SEEK_SET:
buf->off = off;
break;
case SEEK_END:
buf->off = buf->size;
if (off != 0)
n_die("nbuf: offset must be 0 with SEEK_END\n");
break;
default:
n_die("nbuf: unknown whence (%d)\n", whence);
break;
}
return buf->off;
}
int n_buf_write_ex(tn_buf *buf, const void *ptr, int size, int zero)
{
if (zero)
zero = 1;
/* size + zero + 1 - gets() must guarantee that buf is longer
than returned string */
if (buf->allocated - buf->off < (unsigned)size + zero + 1)
if (n_buf_grow(buf, buf->allocated + size + zero + 1) == NULL)
return -1;
memcpy(&buf->data[buf->off], ptr, size);
buf->off += size;
if (buf->off > buf->size)
buf->size = buf->off;
if (zero)
buf->data[buf->off] = '\0';
return size;
}
void n_buf_it_init(tn_buf_it *bufi, tn_buf *buf)
{
bufi->nbuf = buf;
bufi->offs = 0;
}
#if 0
void *n_buf_it_get(tn_buf_it *bufi, size_t size)
{
unsigned char *ptr;
register int boff = bufi->offs;
if (boff + size > bufi->nbuf->size)
return NULL;
ptr = &bufi->nbuf->data[boff];
boff += size;
bufi->offs = boff;
return ptr;
}
#endif
char *n_buf_it_gets_ext(tn_buf_it *bufi, size_t *len, int endl)
{
unsigned char *ptr;
tn_buf *buf;
buf = bufi->nbuf;
if (bufi->offs + 1 > buf->size)
return NULL;
ptr = &buf->data[bufi->offs];
*len = bufi->offs;
while (bufi->offs < buf->size && buf->data[bufi->offs] != endl)
bufi->offs++;
*len = bufi->offs - *len;
bufi->offs++; /* skip endl */
return (char *)ptr;
}
#undef n_buf_add
// legacy: provide n_buf_add symbol
int n_buf_add(tn_buf *buf, const void *data, int size)
{
return n_buf_write_ex(buf, data, size, 0);
}
#undef n_buf_addata
// legacy: provide n_buf_add symbol
int n_buf_addata(tn_buf *buf, const void *data, int size, int zero)
{
return n_buf_write_ex(buf, data, size, zero);
}
#undef n_buf_addstring
// legacy: provide n_buf_add symbol
int n_buf_addstring(tn_buf *buf, const char *str, int zero)
{
return n_buf_write_ex(buf, str, strlen(str), zero);
}
int n_buf_vprintf(tn_buf *nbuf, const char *fmt, va_list args)
{
char buf[4096];
int n;
n = n_vsnprintf(buf, sizeof(buf), fmt, args);
n_buf_write_ex(nbuf, buf, n, 1);
//n_buf_add(nbuf, buf, n);
return n;
}
int n_buf_printf(tn_buf *nbuf, const char *fmt, ...)
{
va_list args;
int n;
va_start(args, fmt);
n = n_buf_vprintf(nbuf, fmt, args);
va_end(args);
return n;
}
#include <netinet/in.h>
#define hton16(v) htons(v)
#define hton32(v) htonl(v)
#define ntoh16(v) ntohs(v)
#define ntoh32(v) ntohl(v)
int n_buf_store(tn_buf *nbuf, tn_stream *st, int sizebits)
{
if (sizebits == 0)
n_store_uint32(st, n_buf_size(nbuf));
else {
switch (sizebits) {
case TN_BUF_STORE_8B:
n_assert(0);
break;
case TN_BUF_STORE_16B:
n_assert(n_buf_size(nbuf) < INT16_MAX);
n_stream_write_uint16(st, n_buf_size(nbuf));
break;
case TN_BUF_STORE_32B:
n_assert(n_buf_size(nbuf) < INT32_MAX);
n_stream_write_uint32(st, n_buf_size(nbuf));
break;
}
}
return n_stream_write(st, n_buf_ptr(nbuf), n_buf_size(nbuf)) ==
n_buf_size(nbuf);
}
int n_buf_store_buf(tn_buf *nbuf, tn_buf *tonbuf, int sizebits)
{
if (sizebits == 0)
n_assert(0);
//n_store_uint32(st, n_buf_size(nbuf));
else {
switch (sizebits) {
case TN_BUF_STORE_8B:
n_assert(0);
break;
case TN_BUF_STORE_16B:
n_assert(n_buf_size(nbuf) < INT16_MAX);
n_buf_add_int16(tonbuf, n_buf_size(nbuf));
break;
case TN_BUF_STORE_32B:
n_assert(n_buf_size(nbuf) < INT32_MAX);
n_buf_add_int32(tonbuf, n_buf_size(nbuf));
break;
}
}
return n_buf_write(tonbuf, n_buf_ptr(nbuf), n_buf_size(nbuf));
}
int n_buf_restore_skip(tn_stream *st, int sizebits)
{
uint32_t size;
if (sizebits == 0)
n_restore_uint32(st, &size);
else {
switch (sizebits) {
case TN_BUF_STORE_8B:
{
uint8_t v = 0;
n_stream_read_uint8(st, &v);
size = v;
break;
}
case TN_BUF_STORE_16B:
{
uint16_t v = 0;
n_stream_read_uint16(st, &v);
size = v;
break;
}
case TN_BUF_STORE_32B:
n_stream_read_uint32(st, &size);
break;
default:
n_assert(0);
}
}
return n_stream_seek(st, size, SEEK_CUR);
}
int n_buf_restore_ex(tn_stream *st, tn_buf **bptr, int sizebits,
int (*process_buf)(tn_buf *, void *), void *arg)
{
tn_buf *nbuf;
char *buf;
uint32_t size;
int rc = 0;
if (bptr)
*bptr = NULL;
if (sizebits == 0)
n_restore_uint32(st, &size);
else {
switch (sizebits) {
case TN_BUF_STORE_8B: {
uint8_t v = 0;
n_stream_read_uint8(st, &v);
size = v;
break;
}
case TN_BUF_STORE_16B:
{
uint16_t v = 0;
n_stream_read_uint16(st, &v);
size = v;
break;
}
case TN_BUF_STORE_32B:
n_stream_read_uint32(st, &size);
break;
default:
n_assert(0);
}
}
//printf("n_buf_restore %d, %lu\n", size, n_stream_tell(st));
if (process_buf == NULL) {
buf = n_malloc(size);
} else {
buf = alloca(size);
}
if (n_stream_read(st, buf, size) != (int)size) {
if (process_buf == NULL)
free(buf);
return 0;
}
nbuf = n_buf_new(0);
n_buf_init(nbuf, buf, size);
if (process_buf == NULL) {
nbuf->flags &= ~(TN_BUF_CONSTDATA); /* managed by buffer */
*bptr = nbuf;
rc = 1;
} else {
rc = process_buf(nbuf, arg);
n_buf_free(nbuf);
}
return rc;
}