summaryrefslogtreecommitdiff
path: root/9p/internal.h
diff options
context:
space:
mode:
Diffstat (limited to '9p/internal.h')
-rw-r--r--9p/internal.h47
1 files changed, 36 insertions, 11 deletions
diff --git a/9p/internal.h b/9p/internal.h
index 1bc0e92..61977d4 100644
--- a/9p/internal.h
+++ b/9p/internal.h
@@ -7,15 +7,36 @@
#ifndef _9P_INTERNAL_H_
#define _9P_INTERNAL_H_
+#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include "9p/defs.h"
+#define USE_CONFIG_9P
+#include "config.h"
+static_assert(CONFIG_9P_MAX_ERR_SIZE <= UINT16_MAX);
+
/* C language *****************************************************************/
#define UNUSED(name) /* name __attribute__ ((unused)) */
+/* types **********************************************************************/
+
+struct p9_ctx {
+ enum p9_version version;
+ uint32_t max_msg_size;
+ uint32_t Rerror_overhead;
+
+ uint32_t err_num;
+ char err_msg[CONFIG_9P_MAX_ERR_SIZE];
+};
+
+static inline enum p9_version p9_ctx_version(p9_ctx *ctx) {
+ assert(ctx);
+ return ctx->version;
+}
+
/* vtables ********************************************************************/
typedef bool (*_checksize_fn_t)(uint32_t net_len, uint8_t *net_bytes, uint32_t *mut_net_offset, size_t *mut_host_extra);
@@ -65,26 +86,30 @@ static inline uint64_t decode_u64le(uint8_t *in) {
;
}
-static inline bool is_valid_utf8_without_nul(uint8_t *str, size_t len) {
- uint8_t mask;
+static inline bool _is_valid_utf8(uint8_t *str, size_t len, bool forbid_nul) {
+ uint32_t ch;
uint8_t chlen;
+ assert(str);
for (size_t pos = 0; pos < len;) {
- if ((str[pos] & 0b10000000) == 0b00000000) { mask = 0b01111111; chlen = 1; }
- else if ((str[pos] & 0b11100000) == 0b11000000) { mask = 0b00011111; chlen = 2; }
- else if ((str[pos] & 0b11110000) == 0b11100000) { mask = 0b00001111; chlen = 3; }
- else if ((str[pos] & 0b11111000) == 0b11110000) { mask = 0b00000111; chlen = 4; }
+ 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 (pos + chlen > len || (str[pos] & mask) == 0) return false;
- switch (chlen) {
- case 4: if ((str[pos+3] & 0b11000000) != 0b10000000) return false; /* fallthrough */
- case 3: if ((str[pos+2] & 0b11000000) != 0b10000000) return false; /* fallthrough */
- case 2: if ((str[pos+1] & 0b11000000) != 0b10000000) return false; /* fallthrough */
+ 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)
+
/* marshal utilities **********************************************************/
static inline void encode_u8le(uint8_t in, uint8_t *out) {