diff options
Diffstat (limited to 'lib9p/types.gen')
-rwxr-xr-x | lib9p/types.gen | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/lib9p/types.gen b/lib9p/types.gen index 0de5068..48f8107 100755 --- a/lib9p/types.gen +++ b/lib9p/types.gen @@ -222,7 +222,7 @@ def gen_h(idprefix: str, versions: set[str], structs: list[Struct]) -> str: #ifndef {guard} #define {guard} -#include <stdint.h> +#include <stdint.h> /* for uint{{n}}_t types */ """ ret += f""" @@ -322,6 +322,9 @@ def gen_c(idprefix: str, versions: set[str], structs: list[Struct]) -> str: ret = f"""/* Generated by `{' '.join(sys.argv)}`. DO NOT EDIT! */ #include <stdbool.h> +#include <stddef.h> /* for size_t */ +#include <inttypes.h> /* for PRI* macros */ +#include <string.h> /* for memset() */ #include <lib9p/9p.h> @@ -390,20 +393,20 @@ static inline bool _checksize_list(struct _checksize_ctx *ctx, # this, but let's make it obvious. ret += "\n" ret += "\tuint32_t base_offset = ctx->net_offset;\n" - ret += "\tif (_checksize_4(ctx))\n" + ret += "\tif (checksize_4(ctx))\n" ret += "\t\treturn true;\n" ret += "\tuint32_t len = decode_u32le(&ctx->net_bytes[base_offset]);\n" - ret += "\treturn checksize_net(ctx, len) || checksize_host(ctx, len);\n" + ret += "\treturn _checksize_net(ctx, len) || _checksize_host(ctx, len);\n" ret += "}\n" case "s": # Add an extra nul-byte on the host, and validate # UTF-8 (also, similar optimization to "d"). ret += "\n" ret += "\tuint32_t base_offset = ctx->net_offset;\n" - ret += "\tif (_checksize_2(ctx))\n" + ret += "\tif (checksize_2(ctx))\n" ret += "\t\treturn true;\n" ret += "\tuint16_t len = decode_u16le(&ctx->net_bytes[base_offset]);\n" - ret += "\tif (checksize_net(ctx, len) || checksize_host(ctx, ((size_t)len)+1))\n" + ret += "\tif (_checksize_net(ctx, len) || _checksize_host(ctx, ((size_t)len)+1))\n" ret += "\t\treturn true;\n" ret += "\tif (!is_valid_utf8_without_nul(&ctx->net_bytes[base_offset+2], len))\n" ret += '\t\treturn lib9p_error(ctx->ctx, LINUX_EBADMSG, "message contains invalid UTF-8");\n' @@ -442,22 +445,22 @@ static inline bool _checksize_list(struct _checksize_ctx *ctx, ret += """ /* unmarshal_* ****************************************************************/ -static inline vold unmarshal_1(struct _unmarshal_ctx *ctx, uint8_t *out) { +static inline void unmarshal_1(struct _unmarshal_ctx *ctx, uint8_t *out) { *out = decode_u8le(&ctx->net_bytes[ctx->net_offset]); ctx->net_offset += 1; } -static inline vold unmarshal_2(struct _unmarshal_ctx *ctx, uint16_t *out) { +static inline void unmarshal_2(struct _unmarshal_ctx *ctx, uint16_t *out) { *out = decode_u16le(&ctx->net_bytes[ctx->net_offset]); ctx->net_offset += 2; } -static inline vold unmarshal_4(struct _unmarshal_ctx *ctx, uint32_t *out) { +static inline void unmarshal_4(struct _unmarshal_ctx *ctx, uint32_t *out) { *out = decode_u32le(&ctx->net_bytes[ctx->net_offset]); ctx->net_offset += 4; } -static inline vold unmarshal_8(struct _unmarshal_ctx *ctx, uint64_t *out) { +static inline void unmarshal_8(struct _unmarshal_ctx *ctx, uint64_t *out) { *out = decode_u64le(&ctx->net_bytes[ctx->net_offset]); ctx->net_offset += 8; } @@ -466,7 +469,7 @@ static inline vold unmarshal_8(struct _unmarshal_ctx *ctx, uint64_t *out) { inline = " inline" if struct.msgid is None else "" argfn = used if struct.members else unused ret += "\n" - ret += f"static{inline} void unmarshal_{struct.name}(struct _unmarshal_ctx *{argfn('ctx')}, {c_typename(idprefix, struct)} *{argfn('out')}) {{\n" + ret += f"static{inline} void unmarshal_{struct.name}(struct _unmarshal_ctx *{argfn('ctx')}, {c_typename(idprefix, struct)} *out) {{\n" ret += "\tmemset(out, 0, sizeof(*out));\n" if struct.members: @@ -489,8 +492,8 @@ static inline vold unmarshal_8(struct _unmarshal_ctx *ctx, uint64_t *out) { if member.cnt: if member.ver != struct_versions: ret += "{\n" - ret += f"{prefix}out->{member.name} = ctx->host_extra;\n" - ret += f"{prefix}ctx->host_extra += sizeof(out->{member.name}[0]) * out->{member.cnt};\n" + ret += f"{prefix}out->{member.name} = ctx->extra;\n" + ret += f"{prefix}ctx->extra += sizeof(out->{member.name}[0]) * out->{member.cnt};\n" ret += f"{prefix}for (typeof(out->{member.cnt}) i = 0; i < out->{member.cnt}; i++)\n" ret += f"{prefix}\tunmarshal_{member.typ.name}(ctx, &out->{member.name}[i]);\n" if member.ver != struct_versions: @@ -504,43 +507,43 @@ static inline vold unmarshal_8(struct _unmarshal_ctx *ctx, uint64_t *out) { /* marshal_* ******************************************************************/ static inline bool _marshal_too_large(struct _marshal_ctx *ctx) { - lib9p_errorf(ctx->ctx, "%s too large to marshal into %s limit (limit=%"PRIu32")", + lib9p_errorf(ctx->ctx, LINUX_EMSGSIZE, "%s too large to marshal into %s limit (limit=%"PRIu32")", (ctx->net_bytes[4] % 2 == 0) ? "T-message" : "R-message", ctx->ctx->version ? "negotiated" : ((ctx->net_bytes[4] % 2 == 0) ? "client" : "server"), - ctx->ctx->max_msg_size)); - return true; + ctx->ctx->max_msg_size); + return true; } static inline bool marshal_1(struct _marshal_ctx *ctx, uint8_t *val) { - if (ctx->net_offset + 1 > ctx->max_msg_size) - return _marshal_too_large(ctx); - out_net_bytes[ctx->net_offset] = *val; + if (ctx->net_offset + 1 > ctx->ctx->max_msg_size) + return _marshal_too_large(ctx); + ctx->net_bytes[ctx->net_offset] = *val; ctx->net_offset += 1; - return false; + return false; } static inline bool marshal_2(struct _marshal_ctx *ctx, uint16_t *val) { - if (ctx->net_offset + 2 > ctx->max_msg_size) - return _marshal_too_large(ctx); - encode_u16le(*val, &out_net_bytes[ctx->net_offset]); + if (ctx->net_offset + 2 > ctx->ctx->max_msg_size) + return _marshal_too_large(ctx); + encode_u16le(*val, &ctx->net_bytes[ctx->net_offset]); ctx->net_offset += 2; - return false; + return false; } static inline bool marshal_4(struct _marshal_ctx *ctx, uint32_t *val) { - if (ctx->net_offset + 4 > ctx->max_msg_size) + if (ctx->net_offset + 4 > ctx->ctx->max_msg_size) return true; - encode_u32le(*val, &out_net_bytes[ctx->net_offset]); + encode_u32le(*val, &ctx->net_bytes[ctx->net_offset]); ctx->net_offset += 4; - return false; + return false; } static inline bool marshal_8(struct _marshal_ctx *ctx, uint64_t *val) { - if (ctx->net_offset + 8 > ctx->max_msg_size) + if (ctx->net_offset + 8 > ctx->ctx->max_msg_size) return true; - encode_u64le(*val, &out_net_bytes[ctx->net_offset]); + encode_u64le(*val, &ctx->net_bytes[ctx->net_offset]); ctx->net_offset += 8; - return false; + return false; } """ for struct in structs: @@ -563,7 +566,7 @@ static inline bool marshal_8(struct _marshal_ctx *ctx, uint64_t *val) { ret += f"\n{prefix }({{" ret += f"\n{prefix2}\tbool err = false;" ret += f"\n{prefix2}\tfor (typeof(val->{member.cnt}) i = 0; i < val->{member.cnt} && !err; i++)" - ret += f"\n{prefix2}\t\terr = marshal_{member.typ.name}(ctx, &val->{member.name}[i]));" + ret += f"\n{prefix2}\t\terr = marshal_{member.typ.name}(ctx, &val->{member.name}[i]);" ret += f"\n{prefix2}\terr;" ret += f"\n{prefix2}}})" else: @@ -575,10 +578,10 @@ static inline bool marshal_8(struct _marshal_ctx *ctx, uint64_t *val) { def msg_entry(msg: Struct) -> str: ret = "" ret += f"\t\t[{idprefix.upper()}TYP_{msg.name}] = {{\n" - ret += f"\t\t\tr.unmarshal_basesize = sizeof({c_typename(idprefix, msg)}),\n" - ret += f"\t\t\tr.unmarshal_extrasize = checksize_{msg.name},\n" - ret += f"\t\t\tr.unmarshal = unmarshal_{msg.name},\n" - ret += f"\t\t\tr.marshal = (_marshal_fn_t)marshal_{msg.name},\n" + ret += f"\t\t\t.unmarshal_basesize = sizeof({c_typename(idprefix, msg)}),\n" + ret += f"\t\t\t.unmarshal_extrasize = checksize_{msg.name},\n" + ret += f"\t\t\t.unmarshal = (_unmarshal_fn_t)unmarshal_{msg.name},\n" + ret += f"\t\t\t.marshal = (_marshal_fn_t)marshal_{msg.name},\n" ret += "\t\t}" return ret |