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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
#!/usr/bin/env python
# 9p/defs.gen - Generate C marshalers/unmarshalers for a .txt file
# defining a 9P protocol variant.
#
# Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-Licence-Identifier: AGPL-3.0-or-later
import enum
import os.path
import re
PROGRAM = "./9p/defs.gen"
# Parse the *.txt ##############################################################
class Atom(enum.Enum):
u8 = 1
u16 = 2
u32 = 4
u64 = 8
@property
def name(self) -> str:
return str(self.value)
@property
def static_size(self) -> int:
return self.value
# `msgid/structname = "member1 member2..."`
# `structname = "member1 member2..."`
# `structname += "member1 member2..."`
class Struct:
msgid: int | None = None
name: str
members: list["Member"]
@property
def static_size(self) -> int | None:
size = 0
for member in self.members:
msize = member.static_size
if msize is None:
return None
size += msize
return size
# `cnt*(name[typ])`
# the `cnt*(...)` wrapper is optional
class Member:
cnt: str | None = None
name: str
typ: Atom | Struct
@property
def static_size(self) -> int | None:
if self.cnt:
return None
return self.typ.static_size
re_membername = "(?:[a-zA-Z_][a-zA-Z_0-9]*)"
re_memberspec = (
f"(?:(?P<cnt>{re_membername})\\*\\()?(?P<name>{re_membername})\\[(?P<typ>.*)\\]\\)?"
)
def parse_members(
env: dict[str, Atom | Struct], existing: list[Member], specs: str
) -> list[Member]:
ret = existing
for spec in specs.split():
m = re.fullmatch(re_memberspec, spec)
if not m:
raise SyntaxError(f"invalid member spec {repr(spec)}")
member = Member()
member.name = m.group("name")
if any(x.name == member.name for x in ret):
raise ValueError(f"duplicate member name {repr(member.name)}")
if m.group("typ") not in env:
raise NameError(f"Unknown type {repr(m.group(2))}")
member.typ = env[m.group("typ")]
if cnt := m.group("cnt"):
if len(ret) == 0 or ret[-1].name != cnt:
raise ValueError(f"list count must be previous item: {repr(cnt)}")
if not isinstance(ret[-1].typ, Atom):
raise ValueError(f"list count must be an integer type: {repr(cnt)}")
member.cnt = cnt
ret += [member]
return ret
re_version = r'version\s+"(?P<version>[^"]+)"'
re_structspec = (
r'(?:(?P<msgid>[0-9]+)/)?(?P<name>\S+)\s*(?P<op>\+?=)\s*"(?P<members>[^"]*)"'
)
re_structspec_cont = r'"(?P<members>[^"]*)"'
def parse_file(filename: str) -> tuple[str, list[Struct]]:
version: str | None = None
env: dict[str, Atom | Struct] = {
"1": Atom.u8,
"2": Atom.u16,
"4": Atom.u32,
"8": Atom.u64,
}
with open(filename, "r") as fh:
prev: Struct | None = None
for line in fh:
line = line.split("#", 1)[0].strip()
if not line:
continue
if m := re.fullmatch(re_version, line):
if version:
raise SyntaxError("must have exactly 1 version line")
version = m.group("version")
elif m := re.fullmatch(re_structspec, line):
if m.group("op") == "+=" and m.group("msgid"):
raise SyntaxError("cannot += to a message that is not yet defined")
match m.group("op"):
case "=":
struct = Struct()
if m.group("msgid"):
struct.msgid = int(m.group("msgid"))
struct.name = m.group("name")
struct.members = parse_members(env, [], m.group("members"))
env[struct.name] = struct
prev = struct
case "+=":
if m.group("name") not in env:
raise NameError(f"Unknown type {repr(m.group('name'))}")
_struct = env[m.group("name")]
if not isinstance(_struct, Struct):
raise NameError(
f"Type {repr(m.group('name'))} is not a struct"
)
struct = _struct
struct.members = parse_members(
env, struct.members, m.group("members")
)
prev = struct
elif m := re.fullmatch(re_structspec_cont, line):
if not prev:
raise SyntaxError("continuation line must come after a struct line")
prev.members = parse_members(env, prev.members, m.group("members"))
else:
raise SyntaxError(f"invalid line {repr(line)}")
if not version:
raise SyntaxError("must have exactly 1 version line")
structs = [x for x in env.values() if isinstance(x, Struct)]
return version, structs
# Generate C ###################################################################
def c_typename(idprefix: str, typ: Atom | Struct) -> str:
match typ:
case Atom():
return f"uint{typ.value*8}_t"
case Struct():
if typ.msgid is not None:
return f"struct {idprefix}msg_{typ.name}"
return f"struct {idprefix}{typ.name}"
case _:
raise ValueError(f"not a type: {typ.__class__.__name__}")
def gen_h(txtname: str, idprefix: str, structs: list[Struct]) -> str:
guard = (
f"_{txtname.replace('.txt', '.h').upper().replace('/', '_').replace('.', '_')}_"
)
ret = f"""/* Generated by `{PROGRAM} {txtname}`. DO NOT EDIT! */
#ifndef {guard}
#define {guard}
#define {idprefix.upper()}MIN_MSGLEN 7
"""
ret += """
/* non-message structs ********************************************************/
"""
for struct in structs:
if struct.msgid is not None:
continue
ret += c_typename(idprefix, struct) + " {\n"
typewidth = max(
len(c_typename(idprefix, member.typ)) for member in struct.members
)
for member in struct.members:
ret += f"\t{c_typename(idprefix, member.typ).ljust(typewidth)} {'*' if member.cnt else ' '}{member.name};\n"
ret += "};\n"
ret += """
/* message types **************************************************************/
"""
ret += f"enum {idprefix}msg_type {{ /* uint8_t */\n"
namewidth = max(len(msg.name) for msg in structs if msg.msgid is not None)
for msg in structs:
if msg.msgid is None:
continue
ret += f"\t{idprefix.upper()}TYP_{msg.name.ljust(namewidth)} = {msg.msgid},\n"
ret += "};\n"
ret += """
/* message structs ************************************************************/
"""
for msg in structs:
if msg.msgid is None:
continue
if not msg.members:
ret += c_typename(idprefix, msg) + " {};\n"
continue
ret += c_typename(idprefix, msg) + " {\n"
typewidth = max(len(c_typename(idprefix, member.typ)) for member in msg.members)
for member in msg.members:
ret += f"\t{c_typename(idprefix, member.typ).ljust(typewidth)} {'*' if member.cnt else ' '}{member.name};\n"
ret += "};\n"
ret += f"""
/* tables *********************************************************************/
#define _P9_TYPECODE_FOR_CTYPE(msg) """
for msg in structs:
if msg.msgid is None:
continue
ret += f", \\\n\t\t{c_typename(idprefix, msg)}: {idprefix.upper()}TYP_{msg.name}"
ret += "\n"
ret += "\n"
ret += f"#endif /* {guard} */\n"
return ret
def gen_c(txtname: str, idprefix: str, structs: list[Struct]) -> str:
txtdir, txtbase = os.path.split(txtname)
header = os.path.join(txtdir, "defs-" + txtbase.replace(".txt", ".h"))
ret = f"""/* Generated by `{PROGRAM} {txtname}`. DO NOT EDIT! */
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h> /* for malloc() */
#include "{header}"
#include "9p/internal.h"
"""
def used(arg: str) -> str:
return arg
def unused(arg: str) -> str:
return f"UNUSED({arg})"
# checksize_* ##############################################################
ret += """
/* checksize_* ****************************************************************/
static inline bool _checksize_list(size_t cnt, _checksize_fn_t fn, size_t host_size,
uint32_t net_len, uint8_t *net_bytes, uint32_t *mut_net_offset, size_t *mut_host_extra) {
for (size_t i = 0; i < cnt; i++)
if (__builtin_add_overflow(*mut_host_extra, host_size, mut_host_extra)
|| fn(net_len, net_bytes, mut_net_offset, mut_host_extra))
return true;
return false;
}
static inline bool checksize_1(uint32_t net_len, uint8_t *UNUSED(net_bytes), uint32_t *mut_net_offset, size_t *UNUSED(mut_host_extra)) {
return __builtin_add_overflow(*mut_net_offset, 1, mut_net_offset) || net_len < *mut_net_offset;
}
static inline bool checksize_2(uint32_t net_len, uint8_t *UNUSED(net_bytes), uint32_t *mut_net_offset, size_t *UNUSED(mut_host_extra)) {
return __builtin_add_overflow(*mut_net_offset, 2, mut_net_offset) || net_len < *mut_net_offset;
}
static inline bool checksize_4(uint32_t net_len, uint8_t *UNUSED(net_bytes), uint32_t *mut_net_offset, size_t *UNUSED(mut_host_extra)) {
return __builtin_add_overflow(*mut_net_offset, 4, mut_net_offset) || net_len < *mut_net_offset;
}
static inline bool checksize_8(uint32_t net_len, uint8_t *UNUSED(net_bytes), uint32_t *mut_net_offset, size_t *UNUSED(mut_host_extra)) {
return __builtin_add_overflow(*mut_net_offset, 8, mut_net_offset) || net_len < *mut_net_offset;
}
"""
for struct in structs:
inline = ' inline' if struct.msgid is None else ''
argfn = used if struct.members else unused
ret += f"static{inline} bool checksize_{struct.name}(uint32_t {argfn('net_len')}, uint8_t *{argfn('net_bytes')}, uint32_t *{argfn('mut_net_offset')}, size_t *{argfn('mut_host_extra')}) {{"
if len(struct.members) == 0:
ret += "\n\treturn false;\n"
ret += "}\n"
continue
prefix0 = "\treturn "
prefix1 = "\t || "
prefix2 = "\t "
prefix = prefix0
prev_size: int | None = None
for member in struct.members:
if member.cnt is not None:
assert prev_size
ret += f"\n{prefix }_checksize_list(decode_u{prev_size*8}le(&net_bytes[(*mut_net_offset)-{prev_size}]), checksize_{member.typ.name}, sizeof({c_typename(idprefix, member.typ)}),"
ret += f"\n{prefix2} net_len, net_bytes, mut_net_offset, mut_host_extra)"
else:
ret += f"\n{prefix}checksize_{member.typ.name}(net_len, net_bytes, mut_net_offset, mut_host_extra)"
prefix = prefix1
prev_size = member.static_size
if struct.name == "s":
ret += (
f"\n{prefix}__builtin_add_overflow(*mut_host_extra, 1, mut_host_extra)"
)
ret += ";\n}\n"
# unmarshal_* ##############################################################
ret += """
/* unmarshal_* ****************************************************************/
/* checksize_XXX() should be called before unmarshal_XXX(). */
static inline bool unmarshal_1(uint8_t *net_bytes, uint32_t *mut_net_offset, void **UNUSED(mut_host_extra), uint8_t *out) {
*out = decode_u8le(&net_bytes[*mut_net_offset]);
*mut_net_offset += 1;
return false;
}
static inline bool unmarshal_2(uint8_t *net_bytes, uint32_t *mut_net_offset, void **UNUSED(mut_host_extra), uint16_t *out) {
*out = decode_u16le(&net_bytes[*mut_net_offset]);
*mut_net_offset += 2;
return false;
}
static inline bool unmarshal_4(uint8_t *net_bytes, uint32_t *mut_net_offset, void **UNUSED(mut_host_extra), uint32_t *out) {
*out = decode_u32le(&net_bytes[*mut_net_offset]);
*mut_net_offset += 4;
return false;
}
static inline bool unmarshal_8(uint8_t *net_bytes, uint32_t *mut_net_offset, void **UNUSED(mut_host_extra), uint64_t *out) {
*out = decode_u64le(&net_bytes[*mut_net_offset]);
*mut_net_offset += 8;
return false;
}
"""
for struct in structs:
argfn = used if struct.members else unused
ret += f"static inline bool unmarshal_{struct.name}(uint8_t *{argfn('net_bytes')}, uint32_t *{argfn('mut_net_offset')}, void **{argfn('mut_host_extra')}, {c_typename(idprefix, struct)} *{argfn('out')}) {{\n"
for member in struct.members:
if member.cnt:
ret += f"\tout->{member.name} = *mut_host_extra;\n"
ret += f"\t*mut_host_extra += sizeof(*out->{member.name}) * out->{member.cnt};\n"
ret += f"\tfor (typeof(out->{member.cnt}) i = 0; i < out->{member.cnt}; i++)\n"
ret += f"\t\tif (unmarshal_{member.typ.name}(net_bytes, mut_net_offset, mut_host_extra, &(out->{member.name}[i])))\n"
ret += f"\t\t\treturn true;\n"
if struct.name == "s":
ret += f"\tif (!is_valid_utf8_without_nul(out->{member.name}, out->{member.cnt}))\n"
ret += f"\t\treturn true;\n"
ret += f"\tout->{member.name}[out->{member.cnt}] = '\\0';\n"
else:
ret += f"\tif (unmarshal_{member.typ.name}(net_bytes, mut_net_offset, mut_host_extra, &(out->{member.name})))\n"
ret += f"\t\treturn true;\n"
ret += "\treturn false;\n"
ret += "}\n"
# marshal_* ################################################################
ret += """
/* marshal_* ******************************************************************/
static inline bool marshal_1(struct p9_ctx *ctx, uint8_t val, uint8_t *out_net_bytes, uint32_t *mut_net_offset) {
if (*mut_net_offset + 1 > ctx->max_msg_size)
return true;
out_net_bytes[*mut_net_offset] = val;
*mut_net_offset += 1;
return false;
}
static inline bool marshal_2(struct p9_ctx *ctx, uint16_t val, uint8_t *out_net_bytes, uint32_t *mut_net_offset) {
if (*mut_net_offset + 2 > ctx->max_msg_size)
return true;
encode_u16le(val, &out_net_bytes[*mut_net_offset]);
*mut_net_offset += 2;
return false;
}
static inline bool marshal_4(struct p9_ctx *ctx, uint32_t val, uint8_t *out_net_bytes, uint32_t *mut_net_offset) {
if (*mut_net_offset + 4 > ctx->max_msg_size)
return true;
encode_u32le(val, &out_net_bytes[*mut_net_offset]);
*mut_net_offset += 4;
return false;
}
static inline bool marshal_8(struct p9_ctx *ctx, uint64_t val, uint8_t *out_net_bytes, uint32_t *mut_net_offset) {
if (*mut_net_offset + 8 > ctx->max_msg_size)
return true;
encode_u64le(val, &out_net_bytes[*mut_net_offset]);
*mut_net_offset += 8;
return false;
}
"""
for struct in structs:
argfn = used if struct.members else unused
ret += f"static inline bool marshal_{struct.name}(struct p9_ctx *{argfn('ctx')}, {c_typename(idprefix, struct)} {argfn('val')}, uint8_t *{argfn('out_net_bytes')}, uint32_t *{argfn('mut_net_offset')}) {{\n"
for member in struct.members:
if member.cnt:
ret += f"\tfor (typeof(val.{member.cnt}) i = 0; i < val.{member.cnt}; i++)\n"
ret += f"\t\tif (marshal_{member.typ.name}(ctx, val.{member.name}[i], out_net_bytes, mut_net_offset))\n"
ret += f"\t\t\treturn true;\n"
else:
ret += f"\tif (marshal_{member.typ.name}(ctx, val.{member.name}, out_net_bytes, mut_net_offset))\n"
ret += f"\t\treturn true;\n"
ret += "\treturn false;\n"
ret += "}\n"
# tables ###################################################################
ret += """
/* tables *********************************************************************/
"""
for msg in structs:
if msg.msgid is None:
continue
ret += f"static bool _unmarshal_{msg.name}(uint8_t *net_bytes, uint32_t *mut_net_offset, void **mut_host_extra, void *out) {{ return unmarshal_{msg.name}(net_bytes, mut_net_offset, mut_host_extra, ({c_typename(idprefix, msg)} *)out); }}\n"
for msg in structs:
if msg.msgid is None:
continue
ret += f"static bool _marshal_{msg.name}(struct p9_ctx *ctx, void *val, uint8_t *out_net_bytes, uint32_t *mut_net_offset) {{ return marshal_{msg.name}(ctx, *(({c_typename(idprefix, msg)} *)val), out_net_bytes, mut_net_offset); }}\n"
ret += "struct version version_9P2000 = {\n"
ret += "\t.msgs = {\n"
for msg in structs:
if msg.msgid is None:
continue
ret += f"\t\t[{idprefix.upper()}TYP_{msg.name}] = {{ .unmarshal_basesize=sizeof({c_typename(idprefix, msg)}), .unmarshal_extrasize=checksize_{msg.name}, .unmarshal=_unmarshal_{msg.name}, .marshal=_marshal_{msg.name} }},\n"
ret += "\t},\n"
ret += "};\n"
############################################################################
return ret
################################################################################
if __name__ == "__main__":
import sys
for txtname in sys.argv[1:]:
txtdir, txtbase = os.path.split(txtname)
version, structs = parse_file(txtname)
with open(
os.path.join(txtdir, "defs-" + txtbase.replace(".txt", ".h")), "w"
) as fh:
fh.write(gen_h(txtname, "p9_", structs))
with open(
os.path.join(txtdir, "defs-" + txtbase.replace(".txt", ".c")), "w"
) as fh:
fh.write(gen_c(txtname, "p9_", structs))
|