Initial commit
[platform/upstream/ccid.git] / src / openct / buffer.c
1 /*
2  * Buffer handling functions
3  *
4  * Copyright (C) 2003, Olaf Kirch <okir@suse.de>
5  */
6
7 #include <config.h>
8
9 #ifdef HAVE_STRING_H
10 #include <string.h>
11 #endif
12
13 #include <openct/buffer.h>
14
15 void
16 ct_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
23 void
24 ct_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
30 int
31 ct_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
41 int
42 ct_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
54 int
55 ct_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
62 unsigned int
63 ct_buf_avail(ct_buf_t *bp)
64 {
65         return bp->tail - bp->head;
66 }
67
68 void *
69 ct_buf_head(ct_buf_t *bp)
70 {
71         return bp->base + bp->head;
72 }
73