-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuffer.c
199 lines (159 loc) · 4.56 KB
/
buffer.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
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
/*
* This file is part of SID.
*
* Copyright (C) 2017-2020 Red Hat, Inc. All rights reserved.
*
* SID is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* SID is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SID. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base/buffer.h"
#include "base/buffer-type.h"
#include "base/mem.h"
#include <errno.h>
static const struct buffer_type *_buffer_type_registry[] =
{[BUFFER_TYPE_LINEAR] = &buffer_type_linear, [BUFFER_TYPE_VECTOR] = &buffer_type_vector};
static bool _check_buf(struct buffer *buf)
{
struct buffer_stat *stat = &buf->stat;
/* We are checking only limit right now so if no limit, nothing to check as well. */
if (stat->init.limit == 0)
return true;
return (stat->init.limit >= stat->init.size && stat->init.limit >= stat->init.alloc_step &&
stat->init.limit % stat->init.alloc_step == 0);
}
struct buffer *buffer_create(struct buffer_spec *spec, struct buffer_init *init, int *ret_code)
{
struct buffer *buf;
int r = 0;
if (!(buf = malloc(sizeof(*buf)))) {
r = -ENOMEM;
goto out;
}
buf->stat = (struct buffer_stat) {
.spec = *spec,
.init = *init,
.usage = (struct buffer_usage) {0},
};
buf->mem = NULL;
buf->fd = -1;
if (!_check_buf(buf)) {
r = -EINVAL;
goto out;
}
if ((r = _buffer_type_registry[spec->type]->create(buf)) < 0)
goto out;
out:
if (ret_code)
*ret_code = r;
if (r < 0)
return mem_freen(buf);
else
return buf;
}
void buffer_destroy(struct buffer *buf)
{
(void) _buffer_type_registry[buf->stat.spec.type]->destroy(buf);
free(buf);
}
int buffer_reset_init(struct buffer *buf, struct buffer_init *init)
{
struct buffer_stat orig_stat = buf->stat;
buf->stat.init = *init;
if (!_check_buf(buf)) {
buf->stat = orig_stat;
return -EINVAL;
}
return _buffer_type_registry[buf->stat.spec.type]->reset(buf);
}
int buffer_reset(struct buffer *buf)
{
return _buffer_type_registry[buf->stat.spec.type]->reset(buf);
}
const void *buffer_add(struct buffer *buf, void *data, size_t len, int *ret_code)
{
return _buffer_type_registry[buf->stat.spec.type]->add(buf, data, len, ret_code);
}
const void *buffer_fmt_add(struct buffer *buf, int *ret_code, const char *fmt, ...)
{
va_list ap;
const void *p;
va_start(ap, fmt);
p = _buffer_type_registry[buf->stat.spec.type]->fmt_add(buf, ret_code, fmt, ap);
va_end(ap);
return p;
}
const void *buffer_vfmt_add(struct buffer *buf, int *ret_code, const char *fmt, va_list ap)
{
return _buffer_type_registry[buf->stat.spec.type]->fmt_add(buf, ret_code, fmt, ap);
}
int buffer_rewind(struct buffer *buf, size_t pos, buffer_pos_t whence)
{
if (whence == BUFFER_POS_REL) {
if (pos == 0)
return 0; /* otherwise this fails on an empty size-prefixed buffer */
if (pos > buf->stat.usage.used)
return -EINVAL;
pos = buf->stat.usage.used - pos;
}
return _buffer_type_registry[buf->stat.spec.type]->rewind(buf, pos);
}
int buffer_rewind_mem(struct buffer *buf, const void *mem)
{
if (mem < buf->mem)
return -EINVAL;
return _buffer_type_registry[buf->stat.spec.type]->rewind_mem(buf, mem);
}
bool buffer_is_complete(struct buffer *buf, int *ret_code)
{
return _buffer_type_registry[buf->stat.spec.type]->is_complete(buf, ret_code);
}
int buffer_get_data(struct buffer *buf, const void **data, size_t *data_size)
{
return _buffer_type_registry[buf->stat.spec.type]->get_data(buf, data, data_size);
}
int buffer_get_fd(struct buffer *buf)
{
return _buffer_type_registry[buf->stat.spec.type]->get_fd(buf);
}
ssize_t buffer_read(struct buffer *buf, int fd)
{
return _buffer_type_registry[buf->stat.spec.type]->read(buf, fd);
}
ssize_t buffer_write(struct buffer *buf, int fd, size_t pos)
{
return _buffer_type_registry[buf->stat.spec.type]->write(buf, fd, pos);
}
struct buffer_stat buffer_stat(struct buffer *buf)
{
return buf->stat;
}
int buffer_write_all(struct buffer *buf, int fd)
{
size_t pos;
ssize_t n;
if (!buf || fd < 0)
return -EINVAL;
for (pos = 0;; pos += n) {
n = buffer_write(buf, fd, pos);
if (n < 0) {
if (n == -ENODATA)
break;
if (n == -EAGAIN || n == -EINTR) {
n = 0;
continue;
}
return n;
}
}
return 0;
}