/
bytebuf.c
96 lines (78 loc) · 1.79 KB
/
bytebuf.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
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "core.h"
#include "xassert.h"
#include "log.h"
#include "bytebuf.h"
int bytebuf_init(struct bytebuf* bb, uint8_t *ptr, size_t len)
{
DBG("%s %p %zu\n", __func__, (void*)ptr, len);
if (!bb || !ptr) {
return -EINVAL;
}
memset(bb, 0, sizeof(struct bytebuf));
bb->cookie = BYTEBUF_COOKIE;
if (len) {
bb->buf = malloc(len);
if (!bb->buf) {
return -ENOMEM;
}
memcpy(bb->buf, ptr, len);
}
bb->len = len;
return 0;
}
void bytebuf_free(struct bytebuf* bb)
{
XASSERT(bb, 0);
XASSERT(bb->cookie == BYTEBUF_COOKIE, bb->cookie);
if (bb->buf) {
/* poison the buffer */
memset(bb->buf, 0xee, bb->len);
PTR_FREE(bb->buf);
}
memset(bb, 0, sizeof(struct bytebuf));
}
void bytebuf_array_verify(struct bytebuf_array* bba)
{
XASSERT(bba != NULL, 0);
XASSERT( bba->cookie == BYTEBUF_ARRAY_COOKIE, bba->cookie);
}
int bytebuf_array_init(struct bytebuf_array* bba, size_t max)
{
memset(bba, 0, sizeof(struct bytebuf_array));
bba->list = (struct bytebuf *)calloc(max, sizeof(struct bytebuf));
if (!bba->list) {
return -ENOMEM;
}
bba->cookie = BYTEBUF_ARRAY_COOKIE;
bba->max = max;
bba->len = 0;
return 0;
}
void bytebuf_array_free(struct bytebuf_array* bba)
{
XASSERT( bba->cookie == BYTEBUF_ARRAY_COOKIE, bba->cookie);
for (size_t i=0 ; i<bba->max; i++) {
if (bba->list[i].cookie ) {
bytebuf_free(&bba->list[i]);
}
}
PTR_FREE(bba->list);
memset(bba, 0, sizeof(struct bytebuf_array));
}
int bytebuf_array_emplace_back(struct bytebuf_array* bba, uint8_t* ptr, size_t len)
{
XASSERT( bba->cookie == BYTEBUF_ARRAY_COOKIE, bba->cookie);
if (bba->len+1 > bba->max) {
return -ENOMEM;
}
int err = bytebuf_init(&bba->list[bba->len], ptr, len);
if (err) {
return err;
}
bba->len++;
return 0;
}