-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg.c
275 lines (214 loc) · 6.32 KB
/
msg.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
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
#include <ctype.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "common.h"
#include "conf.h"
#include "cpp.h"
#include "endy.h"
#include "ext.h"
#include "io.h"
#include "msg.h"
#include "tle.h"
static struct {
enum bmm_msg_num num;
char str[BMM_MCHARID];
bool lowered;
} bmm_msg_decl[] = {
#define BMM_MSG_DECLARE(id, n) \
{.num = BMM_MSG_NUM_##id, .str = #id, .lowered = false},
#include "msg_.h"
#undef BMM_MSG_DECLARE
};
__attribute__ ((__pure__))
static size_t bmm_msg_find(enum bmm_msg_num const num) {
for (size_t i = 0; i < nmembof(bmm_msg_decl); ++i)
if (bmm_msg_decl[i].num == num)
return i;
return SIZE_MAX;
}
static void bmm_msg_lower(size_t const i) {
if (!bmm_msg_decl[i].lowered) {
for (size_t j = 0; bmm_msg_decl[i].str[j] != '\0'; ++j)
bmm_msg_decl[i].str[j] = (char) tolower(bmm_msg_decl[i].str[j]);
bmm_msg_decl[i].lowered = true;
}
}
bool bmm_msg_to_str(char const **const ptr, enum bmm_msg_num const num) {
size_t const i = bmm_msg_find(num);
if (i == SIZE_MAX)
return false;
bmm_msg_lower(i);
if (ptr != NULL)
*ptr = bmm_msg_decl[i].str;
return true;
}
bool bmm_msg_from_str(enum bmm_msg_num *const ptr, char const *const str) {
for (size_t i = 0; i < nmembof(bmm_msg_decl); ++i) {
bmm_msg_lower(i);
if (strcmp(bmm_msg_decl[i].str, str) == 0) {
if (ptr != NULL)
*ptr = bmm_msg_decl[i].num;
return true;
}
}
return false;
}
void bmm_msg_spec_def(struct bmm_msg_spec *const spec) {
spec->prio = BMM_MSG_PRIO_LOW;
spec->endy = bmm_endy_get();
spec->tag = BMM_MSG_TAG_SP;
spec->msg.size = 0;
}
enum bmm_io_read bmm_msg_spec_read(struct bmm_msg_spec *const spec,
bmm_msg_reader const f, void *const ptr) {
unsigned char flags;
switch (f(&flags, 1, ptr)) {
case BMM_IO_READ_ERROR:
return BMM_IO_READ_ERROR;
case BMM_IO_READ_EOF:
return BMM_IO_READ_EOF;
}
if (BMM_MASKALL(flags, BMM_MSG_MASK_PRIO))
spec->prio = BMM_MSG_PRIO_HIGH;
else
spec->prio = BMM_MSG_PRIO_LOW;
switch (flags & BMM_MSG_MASK_ENDY) {
case 0:
spec->endy = BMM_ENDY_LITTLE;
break;
case BMM_MSG_MASK_ENDY:
spec->endy = BMM_ENDY_BIG;
break;
default:
BMM_TLE_EXTS(BMM_TLE_NUM_UNIMPL, "Unsupported endianness");
return BMM_IO_READ_ERROR;
}
if (BMM_MASKALL(flags, BMM_MSG_MASK_VAR)) {
size_t const flagsize = (size_t) (flags & BMM_MSG_MASK_VARSIZE);
size_t const presize = $(bmm_power, size_t)(2, flagsize);
unsigned char buf[BMM_MSG_PRESIZE];
switch (f(buf, presize, ptr)) {
case BMM_IO_READ_ERROR:
return BMM_IO_READ_ERROR;
case BMM_IO_READ_EOF:
return BMM_IO_READ_EOF;
}
if (BMM_MASKALL(flags, BMM_MSG_MASK_TAG)) {
spec->tag = BMM_MSG_TAG_LT;
spec->msg.term.e = flagsize;
for (size_t i = 0; i < presize; ++i)
spec->msg.term.buf[i] = buf[i];
} else {
spec->tag = BMM_MSG_TAG_SP;
size_t size = 0;
switch (spec->endy) {
case BMM_ENDY_LITTLE:
for (size_t i = 0; i < presize; ++i)
size |= (size_t) buf[i] << i * CHAR_BIT;
break;
case BMM_ENDY_BIG:
for (size_t i = 0; i < presize; ++i)
size |= (size_t) buf[presize - 1 - i] << i * CHAR_BIT;
break;
}
spec->msg.size = size;
}
} else {
spec->tag = BMM_MSG_TAG_SP;
spec->msg.size = (size_t) (flags & BMM_MSG_MASK_FIXSIZE);
}
return BMM_IO_READ_SUCCESS;
}
bool bmm_msg_spec_write(struct bmm_msg_spec const *const spec,
bmm_msg_writer const f, void *const ptr) {
unsigned char flags = 0;
switch (spec->prio) {
case BMM_MSG_PRIO_LOW:
break;
case BMM_MSG_PRIO_HIGH:
flags |= BMM_MSG_MASK_PRIO;
break;
}
switch (spec->endy) {
case BMM_ENDY_LITTLE:
break;
case BMM_ENDY_MIDDLE:
BMM_TLE_EXTS(BMM_TLE_NUM_UNIMPL, "Unsupported endianness");
return false;
case BMM_ENDY_BIG:
flags |= BMM_MSG_MASK_ENDY;
break;
}
size_t presize;
unsigned char buf[BMM_MSG_PRESIZE];
switch (spec->tag) {
case BMM_MSG_TAG_SP:
{
size_t const size = spec->msg.size;
// TODO Use `popcnt` instead.
#define BMM_MSG_BITS_FIXSIZE 3
if (size < $(bmm_power, size_t)(2, BMM_MSG_BITS_FIXSIZE)) {
flags |= (unsigned char) size & BMM_MSG_MASK_FIXSIZE;
presize = 0;
} else {
size_t const flagsize = $(bmm_clog, size_t)($(bmm_clog, size_t)(size,
$(bmm_power, size_t)(2, CHAR_BIT)), 2);
flags |= BMM_MSG_MASK_VAR;
flags |= (unsigned char) flagsize & BMM_MSG_MASK_VARSIZE;
presize = $(bmm_power, size_t)(2, flagsize);
switch (spec->endy) {
case BMM_ENDY_LITTLE:
for (size_t i = 0; i < presize; ++i)
buf[i] = (unsigned char) (size >> i * CHAR_BIT & 0xff);
break;
case BMM_ENDY_BIG:
for (size_t i = 0; i < presize; ++i)
buf[presize - 1 - i] = (unsigned char)
(size >> i * CHAR_BIT & 0xff);
break;
}
}
}
break;
case BMM_MSG_TAG_LT:
{
size_t const flagsize = spec->msg.term.e;
flags |= BMM_MSG_MASK_TAG;
flags |= BMM_MSG_MASK_VAR;
flags |= (unsigned char) flagsize & BMM_MSG_MASK_VARSIZE;
presize = $(bmm_power, size_t)(2, flagsize);
dynamic_assert(presize <= BMM_MSG_PRESIZE, "Buffer would overflow");
for (size_t i = 0; i < presize; ++i)
buf[i] = spec->msg.term.buf[i];
}
break;
}
if (!f(&flags, 1, ptr))
return false;
if (!f(buf, presize, ptr))
return false;
return true;
}
enum bmm_io_read bmm_msg_num_read(enum bmm_msg_num *const num,
bmm_msg_reader const f, void *const ptr) {
unsigned char buf;
switch (f(&buf, 1, ptr)) {
case BMM_IO_READ_ERROR:
return BMM_IO_READ_ERROR;
case BMM_IO_READ_EOF:
return BMM_IO_READ_EOF;
}
*num = (enum bmm_msg_num) buf;
return BMM_IO_READ_SUCCESS;
}
bool bmm_msg_num_write(enum bmm_msg_num const *const num,
bmm_msg_writer const f, void *const ptr) {
dynamic_assert(*num <= 0xff, "Type would be truncated");
unsigned char buf = (unsigned char) *num;
if (!f(&buf, 1, ptr))
return false;
return true;
}