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
|
/** \file
* Safe and easy access to memory buffer.
*/
#ifndef _USUAL_MBUF_H_
#define _USUAL_MBUF_H_
#include <usual/base.h>
#include <string.h>
/** MBuf structure. Allocated by user, can be in stack. */
struct MBuf {
uint8_t *data;
unsigned read_pos;
unsigned write_pos;
unsigned alloc_len;
bool reader;
bool fixed;
};
/** Format fragment for *printf() */
#define MBUF_FMT ".*s"
/** Argument layout for *printf() */
#define MBUF_ARG(m) ((m) ? mbuf_written(m) : 6), ((m) ? (const char *)mbuf_data(m) : "(null)")
/*
* Init functions
*/
/** Initialize R/O buffer to fixed memory area. */
static inline void mbuf_init_fixed_reader(struct MBuf *buf, const void *ptr, unsigned len)
{
buf->data = (uint8_t *)ptr;
buf->read_pos = 0;
buf->write_pos = len;
buf->alloc_len = len;
buf->reader = true;
buf->fixed = true;
}
/** Initialize R/W buffer to fixed memory area. */
static inline void mbuf_init_fixed_writer(struct MBuf *buf, void *ptr, unsigned len)
{
buf->data = (uint8_t *)ptr;
buf->read_pos = 0;
buf->write_pos = 0;
buf->alloc_len = len;
buf->reader = false;
buf->fixed = true;
}
/** Initialize R/W buffer to dynamically allocated memory area. */
static inline void mbuf_init_dynamic(struct MBuf *buf)
{
buf->data = NULL;
buf->read_pos = 0;
buf->write_pos = 0;
buf->alloc_len = 0;
buf->reader = false;
buf->fixed = false;
}
/** Free dynamically allocated area, if exists. */
static inline void mbuf_free(struct MBuf *buf)
{
if (buf->data) {
if (!buf->fixed)
free(buf->data);
memset(buf, 0, sizeof(*buf));
}
}
/*
* Reset functions.
*/
/** Move read cursor to start of buffer. */
static inline void mbuf_rewind_reader(struct MBuf *buf)
{
buf->read_pos = 0;
}
/** Move both read and write cursor to start of buffer. */
static inline void mbuf_rewind_writer(struct MBuf *buf)
{
if (!buf->reader) {
buf->read_pos = 0;
buf->write_pos = 0;
}
}
/*
* Info functions.
*/
/** How many bytes can be read with read cursor. */
static inline unsigned mbuf_avail_for_read(const struct MBuf *buf)
{
return buf->write_pos - buf->read_pos;
}
/** How many bytes can be written with write cursor, without realloc. */
static inline unsigned mbuf_avail_for_write(const struct MBuf *buf)
{
if (!buf->reader && buf->alloc_len > buf->write_pos)
return buf->alloc_len - buf->write_pos;
return 0;
}
/** How many data bytes are in buffer. */
static inline unsigned mbuf_written(const struct MBuf *buf)
{
return buf->write_pos;
}
/** How many bytes have been read from buffer */
static inline unsigned mbuf_consumed(const struct MBuf *buf)
{
return buf->read_pos;
}
/** Return pointer to data area. */
static inline const void *mbuf_data(const struct MBuf *buf)
{
return buf->data;
}
/** Do the mbufs contain same data. */
static inline bool mbuf_eq(const struct MBuf *buf1, const struct MBuf *buf2)
{
if (buf1 == buf2) return true;
if (!buf1 || !buf2 || (mbuf_written(buf1) != mbuf_written(buf2)))
return false;
return memcmp(mbuf_data(buf1), mbuf_data(buf2), mbuf_written(buf1)) == 0;
}
/** Complare mbuf to asciiz string */
static inline bool mbuf_eq_str(const struct MBuf *buf1, const char *s)
{
struct MBuf tmp;
mbuf_init_fixed_reader(&tmp, s, strlen(s));
return mbuf_eq(buf1, &tmp);
}
/*
* Read functions.
*/
/** Read a byte from read cursor. */
_MUSTCHECK
static inline bool mbuf_get_byte(struct MBuf *buf, uint8_t *dst_p)
{
if (buf->read_pos + 1 > buf->write_pos)
return false;
*dst_p = buf->data[buf->read_pos++];
return true;
}
/** Read big-endian uint16 from read cursor. */
_MUSTCHECK
static inline bool mbuf_get_char(struct MBuf *buf, char *dst_p)
{
if (buf->read_pos + 1 > buf->write_pos)
return false;
*dst_p = buf->data[buf->read_pos++];
return true;
}
_MUSTCHECK
static inline bool mbuf_get_uint16be(struct MBuf *buf, uint16_t *dst_p)
{
unsigned a, b;
if (buf->read_pos + 2 > buf->write_pos)
return false;
a = buf->data[buf->read_pos++];
b = buf->data[buf->read_pos++];
*dst_p = (a << 8) | b;
return true;
}
/** Read big-endian uint32 from read cursor. */
_MUSTCHECK
static inline bool mbuf_get_uint32be(struct MBuf *buf, uint32_t *dst_p)
{
unsigned a, b, c, d;
if (buf->read_pos + 4 > buf->write_pos)
return false;
a = buf->data[buf->read_pos++];
b = buf->data[buf->read_pos++];
c = buf->data[buf->read_pos++];
d = buf->data[buf->read_pos++];
*dst_p = (a << 24) | (b << 16) | (c << 8) | d;
return true;
}
/** Get reference to len bytes from read cursor. */
_MUSTCHECK
static inline bool mbuf_get_uint64be(struct MBuf *buf, uint64_t *dst_p)
{
uint32_t a, b;
if (!mbuf_get_uint32be(buf, &a)
|| !mbuf_get_uint32be(buf, &b))
return false;
*dst_p = ((uint64_t)a << 32) | b;
return true;
}
_MUSTCHECK
static inline bool mbuf_get_bytes(struct MBuf *buf, unsigned len, const uint8_t **dst_p)
{
if (buf->read_pos + len > buf->write_pos)
return false;
*dst_p = buf->data + buf->read_pos;
buf->read_pos += len;
return true;
}
/** Get reference to asciiz string from read cursor. */
_MUSTCHECK
static inline bool mbuf_get_chars(struct MBuf *buf, unsigned len, const char **dst_p)
{
if (buf->read_pos + len > buf->write_pos)
return false;
*dst_p = (char *)buf->data + buf->read_pos;
buf->read_pos += len;
return true;
}
_MUSTCHECK
static inline bool mbuf_get_string(struct MBuf *buf, const char **dst_p)
{
const char *res = (char *)buf->data + buf->read_pos;
const uint8_t *nul = memchr(res, 0, mbuf_avail_for_read(buf));
if (!nul)
return false;
*dst_p = res;
buf->read_pos = nul + 1 - buf->data;
return true;
}
/*
* Write functions.
*/
/** Allocate more room if needed and the mbuf allows. */
_MUSTCHECK
bool mbuf_make_room(struct MBuf *buf, unsigned len);
/** Write a byte to write cursor. */
_MUSTCHECK
static inline bool mbuf_write_byte(struct MBuf *buf, uint8_t val)
{
if (buf->write_pos + 1 > buf->alloc_len
&& !mbuf_make_room(buf, 1))
return false;
buf->data[buf->write_pos++] = val;
return true;
}
/** Write len bytes to write cursor. */
_MUSTCHECK
static inline bool mbuf_write(struct MBuf *buf, const void *ptr, unsigned len)
{
if (buf->write_pos + len > buf->alloc_len
&& !mbuf_make_room(buf, len))
return false;
memcpy(buf->data + buf->write_pos, ptr, len);
buf->write_pos += len;
return true;
}
/** writes full contents of another mbuf, without touching it */
_MUSTCHECK
static inline bool mbuf_write_raw_mbuf(struct MBuf *dst, struct MBuf *src)
{
return mbuf_write(dst, src->data, src->write_pos);
}
/** writes partial contents of another mbuf, with touching it */
_MUSTCHECK
static inline bool mbuf_write_mbuf(struct MBuf *dst, struct MBuf *src, unsigned len)
{
const uint8_t *data;
if (!mbuf_get_bytes(src, len, &data))
return false;
if (!mbuf_write(dst, data, len)) {
src->read_pos -= len;
return false;
}
return true;
}
/** Fiil mbuf with byte value */
_MUSTCHECK
static inline bool mbuf_fill(struct MBuf *buf, uint8_t byte, unsigned len)
{
if (buf->write_pos + len > buf->alloc_len
&& !mbuf_make_room(buf, len))
return false;
memset(buf->data + buf->write_pos, byte, len);
buf->write_pos += len;
return true;
}
/** remove some data from mbuf */
_MUSTCHECK
static inline bool mbuf_cut(struct MBuf *buf, unsigned ofs, unsigned len)
{
if (buf->reader)
return false;
if (ofs + len < buf->write_pos) {
unsigned endofs = ofs + len;
memmove(buf->data + ofs, buf->data + endofs, buf->write_pos - endofs);
buf->write_pos -= len;
} else if (ofs < buf->write_pos) {
buf->write_pos = ofs;
}
return true;
}
static inline void mbuf_copy(const struct MBuf *src, struct MBuf *dst)
{
*dst = *src;
}
_MUSTCHECK
static inline bool mbuf_slice(struct MBuf *src, unsigned len, struct MBuf *dst)
{
if (len > mbuf_avail_for_read(src))
return false;
mbuf_init_fixed_reader(dst, src->data + src->read_pos, len);
src->read_pos += len;
return true;
}
#endif
|