1/*
2 * Buffer handling functions
3 *
4 * Copyright (C) 2003, Olaf Kirch <okir@suse.de>
5 */
6
7#ifdef HAVE_CONFIG_H
8#include <config.h>
9#endif
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13#include <openct/buffer.h>
14
15void
16ct_buf_init(ct_buf_t *bp, void *mem, size_t len)
17{
18	memset(bp, 0, sizeof(*bp));
19	bp->base = (unsigned char *) mem;
20	bp->size = len;
21}
22
23void
24ct_buf_set(ct_buf_t *bp, void *mem, size_t len)
25{
26	ct_buf_init(bp, mem, len);
27	bp->tail = len;
28}
29
30int
31ct_buf_get(ct_buf_t *bp, void *mem, size_t len)
32{
33	if (len > bp->tail - bp->head)
34		return -1;
35	if (mem)
36		memcpy(mem, bp->base + bp->head, len);
37	bp->head += len;
38	return len;
39}
40
41int
42ct_buf_put(ct_buf_t *bp, const void *mem, size_t len)
43{
44	if (len > bp->size - bp->tail) {
45		bp->overrun = 1;
46		return -1;
47	}
48	if (mem)
49		memcpy(bp->base + bp->tail, mem, len);
50	bp->tail += len;
51	return len;
52}
53
54int
55ct_buf_putc(ct_buf_t *bp, int byte)
56{
57	unsigned char	c = byte;
58
59	return ct_buf_put(bp, &c, 1);
60}
61
62unsigned int
63ct_buf_avail(ct_buf_t *bp)
64{
65	return bp->tail - bp->head;
66}
67
68void *
69ct_buf_head(ct_buf_t *bp)
70{
71	return bp->base + bp->head;
72}
73
74