diff options
-rw-r--r-- | lib9p/tables.c | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/lib9p/tables.c b/lib9p/tables.c index 34ac1af..7b46fb4 100644 --- a/lib9p/tables.c +++ b/lib9p/tables.c @@ -11,28 +11,37 @@ #include "tables.h" -const char *lib9p_version_str(enum lib9p_version ver) { +/* bounds checks **************************************************************/ + +static inline void assert_ver(enum lib9p_version ver) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtype-limits" assert(0 <= ver && ver < LIB9P_VER_NUM); #pragma GCC diagnostic pop - return _lib9p_table_ver_name[ver]; } -uint32_t lib9p_version_min_msg_size(enum lib9p_version ver) { +static inline void assert_typ(enum lib9p_msg_type typ) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtype-limits" - assert(0 <= ver && ver < LIB9P_VER_NUM); + assert(0 <= typ && typ < 0xFF); #pragma GCC diagnostic pop +} + +/* simple lookups *************************************************************/ + +const char *lib9p_version_str(enum lib9p_version ver) { + assert_ver(ver); + return _lib9p_table_ver_name[ver]; +} + +uint32_t lib9p_version_min_msg_size(enum lib9p_version ver) { + assert_ver(ver); return _lib9p_table_msg_min_size[ver]; } const char *lib9p_msgtype_str(enum lib9p_version ver, enum lib9p_msg_type typ) { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wtype-limits" - assert(0 <= ver && ver < LIB9P_VER_NUM); - assert(0 <= typ && typ <= 0xFF); -#pragma GCC diagnostic pop + assert_ver(ver); + assert_typ(typ); return _lib9p_table_msg_name[ver][typ] ?: const_byte_str(typ); } @@ -43,6 +52,7 @@ ssize_t _lib9p_validate(uint8_t xxx_low_typ_bit, const char *xxx_errmsg, const struct _lib9p_recv_tentry xxx_table[LIB9P_VER_NUM][0x80], struct lib9p_ctx *ctx, uint8_t *net_bytes) { + assert_ver(ctx->version); /* Inspect the first 5 bytes ourselves. */ uint32_t net_size = uint32le_decode(net_bytes); if (net_size < 5) @@ -74,6 +84,7 @@ static void _lib9p_unmarshal(const struct _lib9p_recv_tentry xxx_table[LIB9P_VER_NUM][0x80], struct lib9p_ctx *ctx, uint8_t *net_bytes, enum lib9p_msg_type *ret_typ, void *ret_body) { + assert_ver(ctx->version); enum lib9p_msg_type typ = net_bytes[4]; *ret_typ = typ; struct _lib9p_recv_tentry tentry = xxx_table[ctx->version][typ/2]; @@ -97,6 +108,8 @@ static bool _lib9p_marshal(const struct _lib9p_send_tentry xxx_table[LIB9P_VER_NUM][0x80], struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body, size_t *ret_iov_cnt, struct iovec *ret_iov, uint8_t *ret_copied) { + assert_ver(ctx->version); + assert_typ(typ); struct _marshal_ret ret = { .net_iov_cnt = 1, .net_iov = ret_iov, |