File: buffer.c

package info (click to toggle)
open-isns 0.100-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,472 kB
  • sloc: ansic: 19,959; sh: 3,211; python: 1,083; perl: 839; makefile: 213
file content (407 lines) | stat: -rw-r--r-- 5,975 bytes parent folder | download | duplicates (6)
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
/*
 * Buffer handling functions
 *
 * Copyright (C) 2003-2007, Olaf Kirch <olaf.kirch@oracle.com>
 */

#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <err.h>
#include <unistd.h>
#include <netinet/in.h> /* ntohl&htonl */
#include <libisns/buffer.h>
#include <libisns/util.h>	/* htonll */

static int	buf_drain(buf_t *bp);

buf_t *
buf_alloc(size_t size)
{
	buf_t	*bp;

	bp = isns_calloc(1, sizeof(*bp));
	buf_init_empty(bp, size);

	return bp;
}

buf_t *
buf_open(const char *filename, int flags)
{
	static const unsigned int buflen = 4096;
	buf_t		*bp;
	int		oerr;

	if (!(bp = isns_calloc(1, sizeof(*bp) + buflen)))
		return NULL;
	buf_init(bp, (bp + 1), buflen);

	switch (flags & O_ACCMODE) {
	case O_RDONLY:
		bp->write_mode = 0;
		break;

	case O_WRONLY:
		bp->write_mode = 1;
		break;

	default:
		errno = EINVAL;
		goto failed;
	}

	if (!filename || !strcmp(filename, "-")) {
		bp->fd = dup(bp->write_mode? 1 : 0);
	} else {
		bp->fd = open(filename, flags, 0666);
	}

	if (bp->fd < 0)
		goto failed;

	return bp;

failed:	oerr = errno;
	isns_free(bp);
	errno = oerr;
	return NULL;
}

buf_t *
buf_dup(const buf_t *src)
{
	buf_t	*bp;

	bp = buf_alloc(src->max_size);
	buf_put(bp, src->base + src->head, src->tail - src->head);

	bp->addr = src->addr;
	bp->addrlen = src->addrlen;
	return bp;
}

void
buf_close(buf_t *bp)
{
	if (bp->write_mode)
		buf_drain(bp);
	if (bp->fd >= 0)
		close(bp->fd);
	bp->fd = -1;
	isns_free(bp);
}

void
buf_free(buf_t *bp)
{
	if (!bp)
		return;
	if (bp->allocated)
		isns_free(bp->base);
	isns_free(bp);
}

void
buf_list_free(buf_t *bp)
{
	buf_t	*next;

	while (bp) {
		next = bp->next;
		buf_free(bp);
		bp = next;
	}
}

void
buf_init(buf_t *bp, void *mem, size_t len)
{
	memset(bp, 0, sizeof(*bp));
	bp->base = (unsigned char *) mem;
	bp->size = len;
	bp->max_size = len;
	bp->fd = -1;
}

void
buf_init_empty(buf_t *bp, size_t len)
{
	memset(bp, 0, sizeof(*bp));
	bp->max_size = len;
	bp->fd = -1;
}

void
buf_set(buf_t *bp, void *mem, size_t len)
{
	buf_init(bp, mem, len);
	bp->tail = len;
}

void
buf_clear(buf_t *bp)
{
	bp->head = bp->tail = 0;
}

static int
buf_fill(buf_t *bp)
{
	int	n;

	if (bp->head || bp->tail)
		buf_compact(bp);

	if (bp->write_mode || bp->fd < 0)
		return 0;

	n = read(bp->fd, bp->base + bp->tail, buf_tailroom(bp));
	if (n < 0) {
		warn("read error");
		return 0;
	}

	bp->tail += n;
	return n;
}

int
buf_drain(buf_t *bp)
{
	int	n;

	if (!bp->write_mode || bp->fd < 0)
		return 0;

	n = write(bp->fd, bp->base + bp->head, buf_avail(bp));
	if (n < 0) {
		warn("write error");
		return 0;
	}

	bp->head += n;
	return n;
}

int
__buf_resize(buf_t *bp, size_t new_size)
{
	void *new_base;

	if (new_size > bp->max_size)
		return 0;
	isns_assert(bp->allocated || bp->base == NULL);

	new_size = (new_size + 127) & ~127;
	if (new_size > bp->max_size)
		new_size = bp->max_size;

	new_base = isns_realloc(bp->base, new_size);
	if (new_base == NULL)
		return 0;

	bp->base = new_base;
	bp->size = new_size;
	bp->allocated = 1;
	return new_size;
}

buf_t *
buf_split(buf_t **to_split, size_t size)
{
	buf_t *old = *to_split, *new;
	size_t avail;

	avail = buf_avail(old);
	if (size > avail)
		return NULL;

	if (size == avail) {
		*to_split = NULL;
		return old;
	}

	new = buf_alloc(size);
	buf_put(new, buf_head(old), size);
	buf_pull(old, size);

	return new;
}

int
buf_seek(buf_t *bp, off_t offset)
{
	if (bp->write_mode && !buf_drain(bp))
		return 0;
	if (lseek(bp->fd, offset, SEEK_SET) < 0) {
		warn("cannot seek to offset %ld", (long) offset);
		return 0;
	}
	return 1;
}

int
buf_get(buf_t *bp, void *mem, size_t len)
{
	caddr_t		dst = (caddr_t) mem;
	unsigned int	total = len, copy;

	while (len) {
		if ((copy = buf_avail(bp)) > len)
			copy = len;
		if (copy == 0) {
			if (!buf_fill(bp))
				return 0;
			continue;
		}
		if (dst) {
			memcpy(dst, bp->base + bp->head, copy);
			dst += copy;
		}
		bp->head += copy;
		len -= copy;
	}
	return total;
}

int
buf_get32(buf_t *bp, uint32_t *vp)
{
	if (!buf_get(bp, vp, 4))
		return 0;
	*vp = ntohl(*vp);
	return 1;
}

int
buf_get64(buf_t *bp, uint64_t *vp)
{
	if (!buf_get(bp, vp, 8))
		return 0;
	*vp = ntohll(*vp);
	return 1;
}

int
buf_gets(buf_t *bp, char *stringbuf, size_t size)
{
	uint32_t	len, copy;

	if (size == 0)
		return 0;

	if (!buf_get32(bp, &len))
		return 0;

	if ((copy = len) >= size)
		copy = size - 1;

	if (!buf_get(bp, stringbuf, copy))
		return 0;
	stringbuf[copy] = '\0';

	/* Pull remaining bytes */
	if (copy != len && !buf_pull(bp, len - copy))
		return 0;

	return copy + 1;
}

int
buf_put(buf_t *bp, const void *mem, size_t len)
{
	caddr_t		src = (caddr_t) mem;
	unsigned int	total = len, copy;

	while (len) {
		if ((copy = bp->size - bp->tail) > len)
			copy = len;
		if (copy == 0) {
			if (buf_drain(bp)) {
				buf_compact(bp);
				continue;
			}
			if (__buf_resize(bp, bp->tail + len)) {
				buf_compact(bp);
				continue;
			}
			return 0;
		}
		if (src) {
			memcpy(bp->base + bp->tail, src, copy);
			src += copy;
		}
		bp->tail += copy;
		len -= copy;
	}
	return total;
}

int
buf_putc(buf_t *bp, int byte)
{
	unsigned char	c = byte;

	return buf_put(bp, &c, 1);
}

int
buf_put32(buf_t *bp, uint32_t val)
{
	val = htonl(val);
	if (!buf_put(bp, &val, 4))
		return 0;
	return 1;
}

int
buf_put64(buf_t *bp, uint64_t val)
{
	val = htonll(val);
	return buf_put(bp, &val, 8);
}

int
buf_puts(buf_t *bp, const char *sp)
{
	uint32_t	len = 0;

	if (sp)
		len = strlen(sp);
	return buf_put32(bp, len) && buf_put(bp, sp, len);
}

void
buf_compact(buf_t *bp)
{
	unsigned int	count;

	if (bp->head == 0)
		return;

	count = bp->tail - bp->head;
	memmove(bp->base, bp->base + bp->head, count);
	bp->tail -= bp->head;
	bp->head  = 0;
}

void
buf_list_append(buf_t **list, buf_t *bp)
{
	bp->next = NULL;
	while (*list)
		list = &(*list)->next;
	*list = bp;
}

int
buf_truncate(buf_t *bp, size_t len)
{
	if (bp->head + len > bp->tail)
		return 0;

	bp->tail = bp->head + len;
	return 1;
}