summaryrefslogtreecommitdiff
path: root/lib9p/9p.c
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2025-01-19 15:53:46 -0700
committerLuke T. Shumaker <lukeshu@lukeshu.com>2025-01-19 15:53:46 -0700
commit104ea21b497171f5a1c4ba80d82337da3f7c2632 (patch)
tree9b5a167833b9caa4f8f829c9bc7a3711a1cd837a /lib9p/9p.c
parenta35db3be439c9a27f0763036cf3d4992ccf893eb (diff)
parent0ab9da9bc3c6cdaef00b7202ba03eff917b44c95 (diff)
Merge branch 'lukeshu/9p-tidy'
Diffstat (limited to 'lib9p/9p.c')
-rw-r--r--lib9p/9p.c62
1 files changed, 51 insertions, 11 deletions
diff --git a/lib9p/9p.c b/lib9p/9p.c
index ecb75fd..51ff2eb 100644
--- a/lib9p/9p.c
+++ b/lib9p/9p.c
@@ -16,6 +16,54 @@
#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);
+}
+
+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) {
@@ -67,14 +115,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
@@ -85,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,
@@ -96,11 +136,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))