summaryrefslogtreecommitdiff
path: root/usr.sbin/npppd/common/bytebuf.c
blob: 872c0f8fe4040ff859f39b79d49d86d324f1edb0 (plain)
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
/* $OpenBSD: bytebuf.c,v 1.4 2012/02/24 06:19:00 guenther Exp $ */
/*-
 * Copyright (c) 2009 Internet Initiative Japan Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/**@file
 * bytebuffer provides 'byte buffer' helper methods.
 *
 * Example:<pre>
 *	bytebuffer *buf = bytebuffer_create(BUFSIZ);
 *	int sz = read(STDIN_FILENO, bytebuffer_pointer(buf),
 *	    bytebuffer_remaining(buf));
 *	if (sz > 0) {
 *	    bytebuffer_put(buf, BYTEBUFFER_PUT_DIRECT, sz);
 *	    bytebuffer_flip(buf);
 *
 *	    sz = write(STDOUT_FILENO, bytebuffer_pointer(buf),
 *		bytebuffer_remaining(buf));
 *	    bytebuffer_compact(buf);
 *	}</pre>
 *
 * @author Yasuoka Masahiko
 * $Id: bytebuf.c,v 1.4 2012/02/24 06:19:00 guenther Exp $
 */
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#ifdef	BYTEBUF_DEBUG
#include <stdio.h>
#endif

#ifndef	BYTEBUF_ASSERT
#ifdef	BYTEBUF_DEBUG
#define	BYTEBUF_ASSERT(cond)						\
	if (!(cond)) {						\
	    fprintf(stderr,					\
		"\nASSERT(" #cond ") failed on %s() at %s:%d.\n"\
		, __func__, __FILE__, __LINE__);		\
	    abort(); 						\
	}
#else
#define	BYTEBUF_ASSERT(cond)
#endif
#endif

#include "bytebuf.h"

struct _bytebuffer {
	/** current position */
	int 	position;
	/** current limit */
	int	limit;
	/** position mark*/
	int 	mark;
	/** capacity of buffer */
	size_t 	capacity;
	/** allocated memory area */
	void	*data;
};

#ifndef	MIN
#define	MIN(m,n)	((m) < (n)? (m) : (n))
#endif

/**
 * Create a bytebuffer and allocate memory area.
 *
 * @param	capacity	Capacity of allocating memory.  If zero or
 *		    negative value is specified, this function don't allocate
 *		    memory.
 */
bytebuffer *
bytebuffer_create(size_t capacity)
{
	bytebuffer *_this = NULL;

	if ((_this = malloc(sizeof(bytebuffer))) == NULL)
		return NULL;

	memset(_this, 0, sizeof(bytebuffer));

	if (capacity > 0) {
		if ((_this->data = malloc(capacity)) == NULL)
			goto fail;
		memset(_this->data, 0, capacity);
		_this->capacity = capacity;
	} else
		_this->capacity = 0;

	_this->limit = _this->capacity;
	_this->mark = -1;
	return _this;
fail:
	if (_this != NULL)
		free(_this);
	return NULL;
}

/**
 * Create a bytebuffer using existing memory area.  This memory area will
 * be freed by bytebuffer's destructor.
 *
 * @data		the pointer to existing memory area
 * @param capacity	capacity of 'data'.
 */
bytebuffer *
bytebuffer_wrap(void *data, size_t capacity)
{
	bytebuffer *_this;

	if ((_this = bytebuffer_create(0)) == NULL)
		return NULL;

	_this->data = data;
	_this->capacity = capacity;
	_this->mark = -1;

	return _this;
}

/**
 * Unwrap memory from bytebuffer.
 *
 * @param _this		the bytebuffer object.
 */
void *
bytebuffer_unwrap(bytebuffer *_this)
{
	void *rval;

	rval = _this->data;
	_this->data = NULL;
	_this->capacity = 0;
	_this->position = 0;
	_this->limit = 0;
	_this->mark = -1;

	return rval;
}

/**
 * Change capacity of this buffer.
 *
 * @param _this		the bytebuffer object.
 * @param capacity	new capacity.
 */
int
bytebuffer_realloc(bytebuffer *_this, size_t capacity)
{
	void *new_data;

	BYTEBUF_ASSERT(_this->limit <= capacity);

	if (_this->limit > capacity) {
		errno = EINVAL;
		return -1;
	}

	if ((new_data = realloc(_this->data, capacity)) == NULL)
		return -1;

	_this->data = new_data;
	if (_this->limit == _this->capacity)
		_this->limit = capacity;
	_this->capacity = capacity;

	return 0;
}

/**
 * Compact this buffer.  the bytes between position and limit are copied
 * to the beginning of the buffer.
 *
 * @param _this		the bytebuffer object.
 */
void
bytebuffer_compact(bytebuffer *_this)
{
	int len;

	len = bytebuffer_remaining(_this);

	if (len <= 0)
		len = 0;
	else if (_this->position != 0)
		memmove(_this->data,
		    (const char *)_this->data + _this->position, (size_t)len);

	_this->position = len;
	_this->limit = _this->capacity;
	_this->mark = -1;
}

static int bytebuffer_direct0;
/**
 * BYTEBUFFER_PUT_DIRECT specifies the data that has been written already by
 * direct access.
 */
const void * BYTEBUFFER_PUT_DIRECT = &bytebuffer_direct0;

/**
 * BYTEBUFFER_GET_DIRECT specifies the data that has been read already by
 * direct access.
 */
void * BYTEBUFFER_GET_DIRECT = &bytebuffer_direct0;

/**
 * Write the given data to the buffer.
 * If buffer is too small, this function returns <code>NULL</code> and
 * <code>errno</code> is <code>ENOBUFS</code>
 *
 * @param _this		the bytebuffer object.
 * @param src		source data.  To specify the data that has been
 *			written already by direct access, use
 *			{@link ::BYTEBUFFER_PUT_DIRECT} for putting the data.
 *			NULL is the same {@link ::BYTEBUFFER_PUT_DIRECT}
 *			at least on this version, but using NULL is deprecated.
 * @param srclen	length of the source data.
 * @see ::BYTEBUFFER_PUT_DIRECT
 */
void *
bytebuffer_put(bytebuffer *_this, const void *src, size_t srclen)
{
	void *rval;

	BYTEBUF_ASSERT(_this != NULL);
	BYTEBUF_ASSERT(srclen > 0);

	if (srclen > bytebuffer_remaining(_this)) {
		errno = ENOBUFS;
		return NULL;
	}
	rval = (char *)_this->data + _this->position;

	if (src != NULL && src != BYTEBUFFER_PUT_DIRECT)
		memcpy(rval, src, srclen);

	_this->position += srclen;

	return rval;
}

/*
 * Transfer data from this buffer to the given destination memory.
 * If the given buffer is too small, this function returns <code>NULL</code>
 * and <code>errno</code> is <code>ENOBUFS</code>
 *
 * @param	dst	pointer of the destination memory.  Specify NULL
 *			to skip transferring the data.
 * @param	dstlne	memory size of the destination.
 */
void *
bytebuffer_get(bytebuffer *_this, void *dst, size_t dstlen)
{
	BYTEBUF_ASSERT(_this != NULL);
	BYTEBUF_ASSERT(dstlen > 0);

	if (dstlen > bytebuffer_remaining(_this)) {
		errno = ENOBUFS;
		return NULL;
	}
	if (dst != NULL && dst != BYTEBUFFER_GET_DIRECT)
		memcpy(dst, (char *)_this->data + _this->position, dstlen);

	_this->position += dstlen;

	return dst;
}

/** Returns this buffer's position */
int
bytebuffer_position(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);

	return _this->position;
}

/** Returns this buffer's limit */
int
bytebuffer_limit(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);

	return _this->limit;
}

/** Returns this buffer's capacity */
int
bytebuffer_capacity(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);

	return _this->capacity;
}

/** Returns a pointer to current position */
void *
bytebuffer_pointer(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);

	return (char *)_this->data + _this->position;
}

/** Returns the number of byte between current position and the limit*/
size_t
bytebuffer_remaining(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);
	BYTEBUF_ASSERT(_this->limit >= _this->position);

	return _this->limit - _this->position;
}

/** Returns whether there are data between current position and the limit */
int
bytebuffer_has_remaining(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);

	return bytebuffer_remaining(_this) > 0;
}

/**
 * Flip this buffer.
 * The limit is set to the position and the position is set zero.
 */
void
bytebuffer_flip(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);

	_this->limit = _this->position;
	_this->position = 0;
	_this->mark = -1;
}

/**
 * Rewind this buffer.
 * The position is set to zero.
 */
void
bytebuffer_rewind(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);

	_this->position = 0;
	_this->mark = -1;
}

/**
 * Clear this buffer.
 * The position is set to zero.
 */
void
bytebuffer_clear(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);

	_this->limit = _this->capacity;
	_this->position = 0;
	_this->mark = -1;
}

/** mark the current position.  */
void
bytebuffer_mark(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);

	_this->mark = _this->position;
}

/** reset the position to the mark.  */
void
bytebuffer_reset(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);
	BYTEBUF_ASSERT(_this->mark >= 0);

	if (_this->mark >= 0)
		_this->position = _this->mark;
}

/**
 * Destroy bytebuffer object.
 */
void
bytebuffer_destroy(bytebuffer *_this)
{
	BYTEBUF_ASSERT(_this != NULL);

	if (_this != NULL) {
		if (_this->data != NULL)
			free(_this->data);
		free(_this);
	}
}