From e6f0bcde3690ceac09f80ba50d4962d53b0c45f8 Mon Sep 17 00:00:00 2001 From: Franz Haas Date: Fri, 4 Oct 2024 22:08:17 +0200 Subject: [PATCH] - added capability to compile VarInt --- construct/core.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/construct/core.py b/construct/core.py index 8f5919f0..57a52928 100644 --- a/construct/core.py +++ b/construct/core.py @@ -1646,6 +1646,39 @@ def _build(self, obj, stream, context, path): def _emitprimitivetype(self, ksy, bitwise): return "vlq_base128_le" + def _emitparse(self, code): + code.append(""" + def parse_varint(io): + acc = [] + while True: + b = byte2int(io.read(1)) + acc.append(b & 0b01111111) + if b & 0b10000000 == 0: + break + num = 0 + for b in reversed(acc): + num = (num << 7) | b + return num + """) + return "parse_varint(io)" + + def _emitbuild(self, code): + code.append(""" + def build_varint(io, obj): + if not isinstance(obj, int): + raise IntegerError(f"value {obj} is not an integer", path=path) + if obj < 0: + raise IntegerError(f"VarInt cannot build from negative number {obj}", path=path) + x = obj + B = bytearray() + while x > 0b01111111: + B.append(0b10000000 | (x & 0b01111111)) + x >>= 7 + B.append(x) + io.write(bytes(B)) + return obj + """) + return "build_varint(io, obj)" @singleton class ZigZag(Construct):