diff options
Diffstat (limited to 'lib9p/protogen/c_validate.py')
-rw-r--r-- | lib9p/protogen/c_validate.py | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/lib9p/protogen/c_validate.py b/lib9p/protogen/c_validate.py index 1630af2..e315b60 100644 --- a/lib9p/protogen/c_validate.py +++ b/lib9p/protogen/c_validate.py @@ -57,17 +57,17 @@ def gen_c_validate(versions: set[str], typs: list[idl.UserType]) -> str: "\t\t/* If needed-net-size overflowed uint32_t, then\n" "\t\t * there's no way that actual-net-size will live up to\n" "\t\t * that. */\n" - '\t\treturn lib9p_error(ctx->ctx, LINUX_EBADMSG, "message is too short for content");\n' - "\tif (net_offset > ctx->net_size)\n" - '\t\treturn lib9p_errorf(ctx->ctx, LINUX_EBADMSG, "message is too short for content (%"PRIu32" > %"PRIu32") @ %d", net_offset, ctx->net_size, __LINE__);\n' + '\t\treturn lib9p_error(ctx, LINUX_EBADMSG, "message is too short for content");\n' + "\tif (net_offset > net_size)\n" + '\t\treturn lib9p_errorf(ctx, LINUX_EBADMSG, "message is too short for content (%"PRIu32" > %"PRIu32") @ %d", net_offset, net_size, __LINE__);\n' ) ret += cutil.macro( "#define VALIDATE_NET_UTF8(n)\n" "\t{\n" "\t\tsize_t len = n;\n" "\t\tVALIDATE_NET_BYTES(len);\n" - "\t\tif (!is_valid_utf8_without_nul(&ctx->net_bytes[net_offset-len], len))\n" - '\t\t\treturn lib9p_error(ctx->ctx, LINUX_EBADMSG, "message contains invalid UTF-8");\n' + "\t\tif (!is_valid_utf8_without_nul(&net_bytes[net_offset-len], len))\n" + '\t\t\treturn lib9p_error(ctx, LINUX_EBADMSG, "message contains invalid UTF-8");\n' "\t}\n" ) ret += cutil.macro( @@ -76,13 +76,13 @@ def gen_c_validate(versions: set[str], typs: list[idl.UserType]) -> str: "\t\t/* If needed-host-size overflowed ssize_t, then there's\n" "\t\t * no way that actual-net-size will live up to\n" "\t\t * that. */\n" - '\t\treturn lib9p_error(ctx->ctx, LINUX_EBADMSG, "message is too short for content");\n' + '\t\treturn lib9p_error(ctx, LINUX_EBADMSG, "message is too short for content");\n' ) - ret += "#define GET_U8LE(off) (ctx->net_bytes[off])\n" - ret += "#define GET_U16LE(off) uint16le_decode(&ctx->net_bytes[off])\n" - ret += "#define GET_U32LE(off) uint32le_decode(&ctx->net_bytes[off])\n" - ret += "#define GET_U64LE(off) uint64le_decode(&ctx->net_bytes[off])\n" + ret += "#define GET_U8LE(off) (net_bytes[off])\n" + ret += "#define GET_U16LE(off) uint16le_decode(&net_bytes[off])\n" + ret += "#define GET_U32LE(off) uint32le_decode(&net_bytes[off])\n" + ret += "#define GET_U64LE(off) uint64le_decode(&net_bytes[off])\n" ret += "#define LAST_U8LE() GET_U8LE(net_offset-1)\n" ret += "#define LAST_U16LE() GET_U16LE(net_offset-2)\n" @@ -181,7 +181,7 @@ def gen_c_validate(versions: set[str], typs: list[idl.UserType]) -> str: act = f"(uint{nbits}_t)GET_U{nbits}LE({lookup_sym(f'&{child.membname}')})" exp = f"(uint{nbits}_t)({c9util.idl_expr(child.val, lookup_sym)})" ret += f"{'\t'*indent_lvl()}if ({act} != {exp})\n" - ret += f'{"\t"*(indent_lvl()+1)}return lib9p_errorf(ctx->ctx, LINUX_EBADMSG, "{path} value is wrong: actual: %"PRIu{nbits}" != correct:%"PRIu{nbits},\n' + ret += f'{"\t"*(indent_lvl()+1)}return lib9p_errorf(ctx, LINUX_EBADMSG, "{path} value is wrong: actual: %"PRIu{nbits}" != correct:%"PRIu{nbits},\n' ret += f"{'\t'*(indent_lvl()+2)}{act}, {exp});\n" if child.max: incr_flush() @@ -196,16 +196,16 @@ def gen_c_validate(versions: set[str], typs: list[idl.UserType]) -> str: act = f"(uint{nbits}_t)GET_U{nbits}LE({lookup_sym(f'&{child.membname}')})" exp = f"(uint{nbits}_t)({c9util.idl_expr(child.max, lookup_sym)})" ret += f"{'\t'*indent_lvl()}if ({act} > {exp})\n" - ret += f'{"\t"*(indent_lvl()+1)}return lib9p_errorf(ctx->ctx, LINUX_EBADMSG, "{path} value is too large: %"PRIu{nbits}" > %"PRIu{nbits},\n' + ret += f'{"\t"*(indent_lvl()+1)}return lib9p_errorf(ctx, LINUX_EBADMSG, "{path} value is too large: %"PRIu{nbits}" > %"PRIu{nbits},\n' ret += f"{'\t'*(indent_lvl()+2)}{act}, {exp});\n" if isinstance(child.typ, idl.Bitfield): incr_flush() nbytes = child.typ.static_size nbits = nbytes * 8 act = f"GET_U{nbits}LE({lookup_sym(f'&{child.membname}')})" - ret += f"{'\t'*indent_lvl()}if ({act} & ~{child.typ.typname}_masks[ctx->ctx->version])\n" - ret += f'{"\t"*(indent_lvl()+1)}return lib9p_errorf(ctx->ctx, LINUX_EBADMSG, "unknown bits in {child.typ.typname} bitfield: %#0{nbytes*2}"PRIx{nbits},\n' - ret += f"{'\t'*(indent_lvl()+2)}{act} & ~{child.typ.typname}_masks[ctx->ctx->version]);\n" + ret += f"{'\t'*indent_lvl()}if ({act} & ~{child.typ.typname}_masks[ctx->version])\n" + ret += f'{"\t"*(indent_lvl()+1)}return lib9p_errorf(ctx, LINUX_EBADMSG, "unknown bits in {child.typ.typname} bitfield: %#0{nbytes*2}"PRIx{nbits},\n' + ret += f"{'\t'*(indent_lvl()+2)}{act} & ~{child.typ.typname}_masks[ctx->version]);\n" def handle( path: idlutil.Path, @@ -261,11 +261,9 @@ def gen_c_validate(versions: set[str], typs: list[idl.UserType]) -> str: ret += "\n" ret += cutil.ifdef_push(1, c9util.ver_ifdef(typ.in_versions)) if typ.typname == "stat": # SPECIAL (stat) - ret += f"static ssize_t validate_{typ.typname}(struct _validate_ctx *ctx, uint32_t *ret_net_size) {{\n" + ret += f"static ssize_t validate_{typ.typname}(struct lib9p_ctx *ctx, uint32_t net_size, uint8_t *net_bytes, uint32_t *ret_net_size) {{\n" else: - ret += ( - f"static ssize_t validate_{typ.typname}(struct _validate_ctx *ctx) {{\n" - ) + ret += f"static ssize_t validate_{typ.typname}(struct lib9p_ctx *ctx, uint32_t net_size, uint8_t *net_bytes) {{\n" ret += "\tuint32_t net_offset = 0;\n" ret += f"\tssize_t host_size = sizeof({c9util.typename(typ)});\n" |