|
| 1 | +# -*- coding: UTF-8 -*- |
| 2 | +"""LEB128 Codec - leb128 (varint) content encoding. |
| 3 | +
|
| 4 | +LEB128 (Little Endian Base 128), also known as varint, writes integers with 7 bits per byte, least significant group |
| 5 | + first, the most significant bit of each byte being set except for the last one. The signed variant (SLEB128) writes |
| 6 | + the integers in two's complement, its last byte being sign-extended. |
| 7 | +
|
| 8 | +When encoding, the text holds the integers in decimal, separated by whitespaces or commas ; when decoding, the |
| 9 | + integers are separated by a space. |
| 10 | +
|
| 11 | +This codec: |
| 12 | +- en/decodes strings from str to str |
| 13 | +- en/decodes strings from bytes to bytes |
| 14 | +- decodes file content to str (read) |
| 15 | +- encodes file content from str to bytes (write) |
| 16 | +
|
| 17 | +Reference: https://en.wikipedia.org/wiki/LEB128 |
| 18 | +""" |
| 19 | +from ..__common__ import * |
| 20 | + |
| 21 | + |
| 22 | +__examples1__ = { |
| 23 | + 'enc(leb128|uleb128|varint|leb-128)': {'0': "\x00", '127': "\x7f", '128': "\x80\x01", '300': "\xac\x02", |
| 24 | + '624485': "\xe5\x8e\x26", '1 2, 3': "\x01\x02\x03", '-1': None, 'a': None}, |
| 25 | + 'dec(leb128)': {'\xe5\x8e\x26\xac\x02': "624485 300", '\x80\x00': "0", '\x80': None}, |
| 26 | + 'enc-dec(leb128)': ["", "0", "1 127 128 16383 16384 18446744073709551615"], |
| 27 | +} |
| 28 | +__examples2__ = { |
| 29 | + 'enc(sleb128|leb128-signed|sleb-128)': {'0': "\x00", '-1': "\x7f", '63': "\x3f", '64': "\xc0\x00", '-64': "\x40", |
| 30 | + '-65': "\xbf\x7f", '-123456': "\xc0\xbb\x78", '1.5': None}, |
| 31 | + 'dec(sleb128)': {'\xc0\xbb\x78\x7f': "-123456 -1", '\xc0': None}, |
| 32 | + 'enc-dec(sleb128)': ["", "0", "-1 63 64 -64 -65 -123456 9223372036854775807"], |
| 33 | +} |
| 34 | +__guess1__ = __guess2__ = [] |
| 35 | + |
| 36 | + |
| 37 | +def __leb128(signed=False, decode=False): |
| 38 | + ename = ["leb128", "sleb128"][signed] |
| 39 | + def _code(text, errors="strict"): |
| 40 | + _h, r = handle_error(ename, errors, decode=decode, kind="token" if not decode else "byte"), [] |
| 41 | + if decode: |
| 42 | + v, k, start = 0, 0, 0 |
| 43 | + for i, x in enumerate(b(text)): |
| 44 | + v, k, start = v | (x & 0x7f) << k, k + 7, start if k else i |
| 45 | + if x < 0x80: |
| 46 | + # the last byte holds the sign bit (0x40) for the signed variant |
| 47 | + r.append(str(v - (1 << k) if signed and x & 0x40 else v)) |
| 48 | + v, k = 0, 0 |
| 49 | + if k: |
| 50 | + # an incomplete integer (most significant bit set in its last byte) cannot be decoded |
| 51 | + r.append(_h(ensure_str(b(text)[start:]), start, " ".join(r))) |
| 52 | + return (s := " ".join(x for x in r if x != "")), len(s) |
| 53 | + for m in re.finditer(r"[^\s,]+", ensure_str(text)): |
| 54 | + t = m.group() |
| 55 | + if not re.match(r"^[-+]?\d+$", t) or not signed and int(t) < 0: |
| 56 | + r.append(_h(t, m.start())) |
| 57 | + else: |
| 58 | + v, out = int(t), [] |
| 59 | + while True: |
| 60 | + x, v = v & 0x7f, v >> 7 |
| 61 | + # with the signed variant, the encoding stops when the remaining bits are a sign extension |
| 62 | + if (v == 0 and not (signed and x & 0x40)) or (signed and v == -1 and x & 0x40): |
| 63 | + out.append(x) |
| 64 | + break |
| 65 | + out.append(x | 0x80) |
| 66 | + r.append("".join(map(chr, out))) |
| 67 | + return (s := "".join(r)), len(s) |
| 68 | + return _code |
| 69 | + |
| 70 | + |
| 71 | +for _n, _s, _p, _ex, _gs in [("leb128", False, r"^(?:u?leb[-_]?128|varint)$", __examples1__, __guess1__), |
| 72 | + ("sleb128", True, r"^(?:sleb[-_]?128|leb[-_]?128[-_]signed)$", __examples2__, __guess2__)]: |
| 73 | + add(_n, __leb128(_s), __leb128(_s, True), _p, examples=_ex, guess=_gs) |
0 commit comments