From 6381e08fd03e322d7d95973ca00c5605afb67707 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sun, 12 Jan 2025 22:33:38 -0700 Subject: lib9p: Consolidate enum-string functions --- lib9p/9p.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'lib9p/9p.c') diff --git a/lib9p/9p.c b/lib9p/9p.c index ecb75fd..de6f05b 100644 --- a/lib9p/9p.c +++ b/lib9p/9p.c @@ -16,6 +16,25 @@ #include "internal.h" +/* strings ********************************************************************/ + +const char *lib9p_version_str(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]; +} + +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 + return _lib9p_table_msg_name[ver][typ] ?: const_byte_str(typ); +} + /* ctx ************************************************************************/ void lib9p_ctx_clear_error(struct lib9p_ctx *ctx) { @@ -67,14 +86,6 @@ int lib9p_errorf(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *fmt, . return -1; } -const char *lib9p_msg_type_str(struct lib9p_ctx *ctx, enum lib9p_msg_type typ) { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wtype-limits" - assert(0 <= typ && typ <= 0xFF); -#pragma GCC diagnostic pop - return _lib9p_table_msg_name[ctx->version][typ] ?: const_byte_str(typ); -} - /* main message functions *****************************************************/ static @@ -96,11 +107,11 @@ ssize_t _lib9p_validate(uint8_t xxx_low_typ_bit, uint8_t typ = net_bytes[4]; if (typ % 2 != xxx_low_typ_bit) return lib9p_errorf(ctx, LINUX_EOPNOTSUPP, "%s: message_type=%s", xxx_errmsg, - lib9p_msg_type_str(ctx, typ)); + lib9p_msgtype_str(ctx->version, typ)); struct _lib9p_recv_tentry tentry = xxx_table[ctx->version][typ/2]; if (!tentry.validate) return lib9p_errorf(ctx, LINUX_EOPNOTSUPP, "unknown message type: %s (protocol_version=%s)", - lib9p_msg_type_str(ctx, typ), lib9p_version_str(ctx->version)); + lib9p_msgtype_str(ctx->version, typ), lib9p_version_str(ctx->version)); /* Now use the message-type-specific tentry to process the whole thing. */ if (tentry.validate(&subctx)) -- cgit v1.2.3-2-g168b From cd9667d6be072a9c736e49c679b55852f7b30a6c Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sun, 12 Jan 2025 23:08:14 -0700 Subject: lib9p: Don't nul-terminate strings, add some string utils --- lib9p/9p.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'lib9p/9p.c') diff --git a/lib9p/9p.c b/lib9p/9p.c index de6f05b..5bea9f9 100644 --- a/lib9p/9p.c +++ b/lib9p/9p.c @@ -35,6 +35,35 @@ const char *lib9p_msgtype_str(enum lib9p_version ver, enum lib9p_msg_type typ) { return _lib9p_table_msg_name[ver][typ] ?: const_byte_str(typ); } +struct lib9p_s lib9p_str(char *s) { + if (!s) + return (struct lib9p_s){0}; + return (struct lib9p_s){ + .len = strlen(s), + .utf8 = s, + }; +} +struct lib9p_s lib9p_strn(char *s, size_t maxlen) { + if (maxlen == 0 || !s) + return (struct lib9p_s){0}; + return (struct lib9p_s){ + .len = strnlen(s, maxlen), + .utf8 = s, + }; +} +struct lib9p_s lib9p_str_slice(struct lib9p_s s, uint16_t beg, uint16_t end) { + assert(s.len == 0 || s.utf8); + assert(beg <= end && end <= s.len); + return (struct lib9p_s){ + .len = end - beg, + .utf8 = &s.utf8[beg], + }; +} +bool lib9p_str_eq(struct lib9p_s a, struct lib9p_s b) { + return a.len == b.len && + (a.len == 0 || memcmp(a.utf8, b.utf8, a.len) == 0); +} + /* ctx ************************************************************************/ void lib9p_ctx_clear_error(struct lib9p_ctx *ctx) { -- cgit v1.2.3-2-g168b From 0ab9da9bc3c6cdaef00b7202ba03eff917b44c95 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sun, 19 Jan 2025 15:32:10 -0700 Subject: lib9p: Use --- lib9p/9p.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib9p/9p.c') diff --git a/lib9p/9p.c b/lib9p/9p.c index 5bea9f9..51ff2eb 100644 --- a/lib9p/9p.c +++ b/lib9p/9p.c @@ -125,7 +125,7 @@ ssize_t _lib9p_validate(uint8_t xxx_low_typ_bit, /* Inspect the first 5 bytes ourselves. */ struct _validate_ctx subctx = { .ctx = ctx, - .net_size = decode_u32le(net_bytes), + .net_size = uint32le_decode(net_bytes), .net_bytes = net_bytes, .net_offset = 0, -- cgit v1.2.3-2-g168b