diff options
Diffstat (limited to 'lib9p')
-rw-r--r-- | lib9p/core_gen/c.py | 2 | ||||
-rw-r--r-- | lib9p/core_gen/c_fmt_print.py | 2 | ||||
-rw-r--r-- | lib9p/core_gen/c_validate.py | 2 | ||||
-rw-r--r-- | lib9p/core_generated.c | 14 | ||||
-rw-r--r-- | lib9p/core_utf8.h | 36 |
5 files changed, 10 insertions, 46 deletions
diff --git a/lib9p/core_gen/c.py b/lib9p/core_gen/c.py index 60ceb70..5776035 100644 --- a/lib9p/core_gen/c.py +++ b/lib9p/core_gen/c.py @@ -29,11 +29,11 @@ def gen_c(versions: set[str], typs: list[idl.UserType]) -> str: #include <libmisc/assert.h> #include <libmisc/endian.h> +#include <libmisc/utf8.h> #include <lib9p/core.h> #include "core_tables.h" -#include "core_utf8.h" """ # utilities ################################################################ ret += """ diff --git a/lib9p/core_gen/c_fmt_print.py b/lib9p/core_gen/c_fmt_print.py index eaacddb..7a0a9d3 100644 --- a/lib9p/core_gen/c_fmt_print.py +++ b/lib9p/core_gen/c_fmt_print.py @@ -112,7 +112,7 @@ def gen_c_fmt_print(versions: set[str], typs: list[idl.UserType]) -> str: cnt_str = f"self->{member.cnt.membname}" cnt_typ = c9util.typename(member.cnt.typ) if member.typ.static_size == 1: # SPECIAL (data) - ret += f"\tif (is_valid_utf8_without_nul((uint8_t *)self->{member.membname}, (size_t){cnt_str})) {{\n" + ret += f"\tif (utf8_is_valid_without_nul((uint8_t *)self->{member.membname}, (size_t){cnt_str})) {{\n" ret += f'\t\tfmt_print_str(w, " {member.membname}=");\n' ret += f"\t\tfmt_print_qmem(w, self->{member.membname}, {cnt_str} < 50 ? {cnt_str} : 50);\n" ret += f"\t\tif ({cnt_str} > 50)\n" diff --git a/lib9p/core_gen/c_validate.py b/lib9p/core_gen/c_validate.py index 9c55d8d..e7a4017 100644 --- a/lib9p/core_gen/c_validate.py +++ b/lib9p/core_gen/c_validate.py @@ -66,7 +66,7 @@ def gen_c_validate(versions: set[str], typs: list[idl.UserType]) -> str: "\t{\n" "\t\tsize_t len = n;\n" "\t\tVALIDATE_NET_BYTES(len);\n" - "\t\tif (!is_valid_utf8_without_nul(&net_bytes[net_offset-len], len))\n" + "\t\tif (!utf8_is_valid_without_nul(&net_bytes[net_offset-len], len))\n" f'\t\t\treturn lib9p_error(ctx, {c9util.IDENT("ERRNO_L_EBADMSG")}, "message contains invalid UTF-8");\n' "\t}\n" ) diff --git a/lib9p/core_generated.c b/lib9p/core_generated.c index 81ace7d..e19f6e6 100644 --- a/lib9p/core_generated.c +++ b/lib9p/core_generated.c @@ -6,11 +6,11 @@ #include <libmisc/assert.h> #include <libmisc/endian.h> +#include <libmisc/utf8.h> #include <lib9p/core.h> #include "core_tables.h" -#include "core_utf8.h" /* utilities ******************************************************************/ #if CONFIG_9P_ENABLE_9P2000 @@ -234,7 +234,7 @@ static const lib9p_lock_flags_t lock_flags_masks[LIB9P_VER_NUM] = { { \ size_t len = n; \ VALIDATE_NET_BYTES(len); \ - if (!is_valid_utf8_without_nul(&net_bytes[net_offset-len], len)) \ + if (!utf8_is_valid_without_nul(&net_bytes[net_offset-len], len)) \ return lib9p_error(ctx, LIB9P_ERRNO_L_EBADMSG, "message contains invalid UTF-8"); \ } #define RESERVE_HOST_BYTES(n) \ @@ -6507,7 +6507,7 @@ static bool marshal_Rswrite(struct lib9p_ctx *ctx, struct lib9p_msg_Rswrite *val fmt_print_tag(w, ctx, &self->tag); fmt_print_str(w, " count="); fmt_print_base10(w, self->count); - if (is_valid_utf8_without_nul((uint8_t *)self->data, (size_t)self->count)) { + if (utf8_is_valid_without_nul((uint8_t *)self->data, (size_t)self->count)) { fmt_print_str(w, " data="); fmt_print_qmem(w, self->data, self->count < 50 ? self->count : 50); if (self->count > 50) @@ -6528,7 +6528,7 @@ static bool marshal_Rswrite(struct lib9p_ctx *ctx, struct lib9p_msg_Rswrite *val fmt_print_base10(w, self->offset); fmt_print_str(w, " count="); fmt_print_base10(w, self->count); - if (is_valid_utf8_without_nul((uint8_t *)self->data, (size_t)self->count)) { + if (utf8_is_valid_without_nul((uint8_t *)self->data, (size_t)self->count)) { fmt_print_str(w, " data="); fmt_print_qmem(w, self->data, self->count < 50 ? self->count : 50); if (self->count > 50) @@ -6763,7 +6763,7 @@ static bool marshal_Rswrite(struct lib9p_ctx *ctx, struct lib9p_msg_Rswrite *val fmt_print_tag(w, ctx, &self->tag); fmt_print_str(w, " count="); fmt_print_base10(w, self->count); - if (is_valid_utf8_without_nul((uint8_t *)self->data, (size_t)self->count)) { + if (utf8_is_valid_without_nul((uint8_t *)self->data, (size_t)self->count)) { fmt_print_str(w, " data="); fmt_print_qmem(w, self->data, self->count < 50 ? self->count : 50); if (self->count > 50) @@ -6846,7 +6846,7 @@ static bool marshal_Rswrite(struct lib9p_ctx *ctx, struct lib9p_msg_Rswrite *val fmt_print_tag(w, ctx, &self->tag); fmt_print_str(w, " count="); fmt_print_base10(w, self->count); - if (is_valid_utf8_without_nul((uint8_t *)self->data, (size_t)self->count)) { + if (utf8_is_valid_without_nul((uint8_t *)self->data, (size_t)self->count)) { fmt_print_str(w, " data="); fmt_print_qmem(w, self->data, self->count < 50 ? self->count : 50); if (self->count > 50) @@ -7445,7 +7445,7 @@ static bool marshal_Rswrite(struct lib9p_ctx *ctx, struct lib9p_msg_Rswrite *val fmt_print_str(w, " ]"); fmt_print_str(w, " count="); fmt_print_base10(w, self->count); - if (is_valid_utf8_without_nul((uint8_t *)self->data, (size_t)self->count)) { + if (utf8_is_valid_without_nul((uint8_t *)self->data, (size_t)self->count)) { fmt_print_str(w, " data="); fmt_print_qmem(w, self->data, self->count < 50 ? self->count : 50); if (self->count > 50) diff --git a/lib9p/core_utf8.h b/lib9p/core_utf8.h deleted file mode 100644 index 2c451e0..0000000 --- a/lib9p/core_utf8.h +++ /dev/null @@ -1,36 +0,0 @@ -/* lib9p/core_utf8.h - Internal UTF-8 validation - * - * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -#ifndef _LIB9P_CORE_UTF8_H_ -#define _LIB9P_CORE_UTF8_H_ - -#include <stddef.h> /* for size_t */ -#include <stdint.h> /* for uint{n}_t */ - -static inline bool _is_valid_utf8(uint8_t *str, size_t len, bool forbid_nul) { - uint32_t ch; - uint8_t chlen; - for (size_t pos = 0; pos < len;) { - if ((str[pos] & 0b10000000) == 0b00000000) { ch = str[pos] & 0b01111111; chlen = 1; } - else if ((str[pos] & 0b11100000) == 0b11000000) { ch = str[pos] & 0b00011111; chlen = 2; } - else if ((str[pos] & 0b11110000) == 0b11100000) { ch = str[pos] & 0b00001111; chlen = 3; } - else if ((str[pos] & 0b11111000) == 0b11110000) { ch = str[pos] & 0b00000111; chlen = 4; } - else return false; - if ((ch == 0 && (chlen != 1 || forbid_nul)) || pos + chlen > len) return false; - for (uint8_t i = 1; i < chlen; i++) { - if ((str[pos+i] & 0b11000000) != 0b10000000) return false; - ch = (ch << 6) | (str[pos+i] & 0b00111111); - } - if (ch > 0x10FFFF) return false; - pos += chlen; - } - return true; -} - -#define is_valid_utf8(str, len) _is_valid_utf8(str, len, false) -#define is_valid_utf8_without_nul(str, len) _is_valid_utf8(str, len, true) - -#endif /* _LIB9P_CORE_UTF8_H_ */ |