2019-08-26 20:35:17 +08:00
|
|
|
import itertools
|
|
|
|
import math
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2019-08-26 20:35:17 +08:00
|
|
|
from libp2p.exceptions import ParseError
|
2019-09-04 13:07:05 +08:00
|
|
|
from libp2p.io.abc import Reader
|
2019-08-15 23:31:26 +08:00
|
|
|
|
2019-09-10 05:12:38 +08:00
|
|
|
from .io.utils import read_exactly
|
|
|
|
|
2019-08-26 20:35:17 +08:00
|
|
|
# Unsigned LEB128(varint codec)
|
|
|
|
# Reference: https://github.com/ethereum/py-wasm/blob/master/wasm/parsers/leb128.py
|
|
|
|
|
|
|
|
LOW_MASK = 2 ** 7 - 1
|
|
|
|
HIGH_MASK = 2 ** 7
|
|
|
|
|
|
|
|
|
|
|
|
# The maximum shift width for a 64 bit integer. We shouldn't have to decode
|
|
|
|
# integers larger than this.
|
|
|
|
SHIFT_64_BIT_MAX = int(math.ceil(64 / 7)) * 7
|
|
|
|
|
2019-08-17 00:19:37 +08:00
|
|
|
|
2019-08-02 18:28:04 +08:00
|
|
|
def encode_uvarint(number: int) -> bytes:
|
2018-11-12 06:15:55 +08:00
|
|
|
"""Pack `number` into varint bytes"""
|
2019-08-01 06:00:12 +08:00
|
|
|
buf = b""
|
2018-11-12 06:15:55 +08:00
|
|
|
while True:
|
2019-08-01 06:00:12 +08:00
|
|
|
towrite = number & 0x7F
|
2018-11-12 06:15:55 +08:00
|
|
|
number >>= 7
|
|
|
|
if number:
|
2019-08-01 06:00:12 +08:00
|
|
|
buf += bytes((towrite | 0x80,))
|
2018-11-12 06:15:55 +08:00
|
|
|
else:
|
2019-08-01 06:00:12 +08:00
|
|
|
buf += bytes((towrite,))
|
2018-11-12 06:15:55 +08:00
|
|
|
break
|
|
|
|
return buf
|
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2019-09-05 23:24:17 +08:00
|
|
|
async def decode_uvarint_from_stream(reader: Reader) -> int:
|
2019-08-26 20:35:17 +08:00
|
|
|
"""
|
|
|
|
https://en.wikipedia.org/wiki/LEB128
|
|
|
|
"""
|
|
|
|
res = 0
|
|
|
|
for shift in itertools.count(0, 7):
|
|
|
|
if shift > SHIFT_64_BIT_MAX:
|
|
|
|
raise ParseError("TODO: better exception msg: Integer is too large...")
|
2018-11-12 06:15:55 +08:00
|
|
|
|
2019-09-13 15:29:24 +08:00
|
|
|
byte = await read_exactly(reader, 1)
|
|
|
|
value = byte[0]
|
2019-08-01 06:00:12 +08:00
|
|
|
|
2019-08-26 20:35:17 +08:00
|
|
|
res += (value & LOW_MASK) << shift
|
2019-08-17 00:19:37 +08:00
|
|
|
|
2019-08-26 20:35:17 +08:00
|
|
|
if not value & HIGH_MASK:
|
|
|
|
break
|
|
|
|
return res
|
2019-08-17 00:19:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
def encode_varint_prefixed(msg_bytes: bytes) -> bytes:
|
|
|
|
varint_len = encode_uvarint(len(msg_bytes))
|
|
|
|
return varint_len + msg_bytes
|
|
|
|
|
|
|
|
|
2019-09-05 23:24:17 +08:00
|
|
|
async def read_varint_prefixed_bytes(reader: Reader) -> bytes:
|
2019-08-26 20:35:17 +08:00
|
|
|
len_msg = await decode_uvarint_from_stream(reader)
|
2019-09-10 05:12:38 +08:00
|
|
|
data = await read_exactly(reader, len_msg)
|
2019-08-20 17:09:38 +08:00
|
|
|
return data
|
2019-08-17 00:19:37 +08:00
|
|
|
|
|
|
|
|
2019-08-17 21:41:17 +08:00
|
|
|
# Delimited read/write, used by multistream-select.
|
|
|
|
# Reference: https://github.com/gogo/protobuf/blob/07eab6a8298cf32fac45cceaac59424f98421bbc/io/varint.go#L109-L126 # noqa: E501
|
2019-08-17 00:19:37 +08:00
|
|
|
|
|
|
|
|
2019-08-20 17:09:38 +08:00
|
|
|
def encode_delim(msg: bytes) -> bytes:
|
|
|
|
delimited_msg = msg + b"\n"
|
|
|
|
return encode_varint_prefixed(delimited_msg)
|
2019-08-17 00:19:37 +08:00
|
|
|
|
|
|
|
|
2019-09-05 23:24:17 +08:00
|
|
|
async def read_delim(reader: Reader) -> bytes:
|
2019-08-20 17:09:38 +08:00
|
|
|
msg_bytes = await read_varint_prefixed_bytes(reader)
|
2019-09-23 16:01:22 +08:00
|
|
|
if len(msg_bytes) == 0 or msg_bytes[-1:] != b"\n":
|
|
|
|
raise ParseError(f'msg_bytes is not delimited by b"\\n": msg_bytes={msg_bytes}')
|
2019-08-20 17:09:38 +08:00
|
|
|
return msg_bytes[:-1]
|
2019-08-17 21:41:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
SIZE_LEN_BYTES = 4
|
|
|
|
|
|
|
|
# Fixed-prefixed read/write, used by "/plaintext/2.0.0".
|
|
|
|
# Reference: https://github.com/libp2p/go-msgio/blob/d5bbf59d3c4240266b1d2e5df9dc993454c42011/num.go#L11-L33 # noqa: E501 # noqa: E501
|
|
|
|
|
|
|
|
|
|
|
|
def encode_fixedint_prefixed(msg_bytes: bytes) -> bytes:
|
|
|
|
len_prefix = len(msg_bytes).to_bytes(SIZE_LEN_BYTES, "big")
|
|
|
|
return len_prefix + msg_bytes
|
|
|
|
|
|
|
|
|
2019-09-04 13:07:05 +08:00
|
|
|
async def read_fixedint_prefixed(reader: Reader) -> bytes:
|
2019-08-17 21:41:17 +08:00
|
|
|
len_bytes = await reader.read(SIZE_LEN_BYTES)
|
|
|
|
len_int = int.from_bytes(len_bytes, "big")
|
|
|
|
return await reader.read(len_int)
|