const lex = @import("lex.zig"); pub const Scalar = enum { U8, U16, U32, U64, pub fn width(self: Scalar) usize { return switch (self) { Scalar.U8 => 1, Scalar.U16 => 2, Scalar.U32 => 4, Scalar.U64 => 8, }; } pub fn pba_name(self: Scalar) []const u8 { return switch (self) { Scalar.U8 => "u8", Scalar.U16 => "u16", Scalar.U32 => "u32", Scalar.U64 => "u64", }; } pub fn c_type(self: Scalar) []const u8 { return switch (self) { Scalar.U8 => "uint8_t", Scalar.U16 => "uint16_t", Scalar.U32 => "uint32_t", Scalar.U64 => "uint64_t", }; } }; pub const ArrayLength = union(enum) { fixed: usize, ref: []const u8, open: void, }; pub const FieldType = union(enum) { single: Scalar, array: struct { scalar_type: Scalar, length: ArrayLength, }, }; pub const Field = struct { name: []const u8, field_type: FieldType, position: lex.Position, }; pub const Builtin = enum { PRECEDING, }; pub const Value = union(enum) { literal: usize, field_ref: []const u8, builtin: Builtin, }; pub const AeadAlgorithm = union(enum) { chacha20_poly1305: struct { nonce: Value, key: Value, aad: Value, }, pub fn tag_len(self: AeadAlgorithm) usize { return switch (self) { .chacha20_poly1305 => 16, }; } }; pub const DigestAlgorithm = enum { SHA256, SHA512, pub fn digest_len(self: DigestAlgorithm) usize { return switch (self) { DigestAlgorithm.SHA256 => 32, DigestAlgorithm.SHA512 => 64, }; } }; pub const Block = struct { contents: union(enum) { clear: []Field, aead: []struct { algorithm: AeadAlgorithm, }, }, pos: lex.Position, }; pub const Message = struct { name: []const u8, params: []Field, blocks: []Block, pos: lex.Position, digest: ?DigestAlgorithm, fragment_key: ?Value, }; pub const ProtocolInstance = struct { version: usize, header: []Field, messages: []Message, pos: lex.Position, }; pub const Protocol = struct { name: []const u8, mtu: usize, instances: []ProtocolInstance, }; const auto_header_len = 6;