Simplify parse_compact_size usage.
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
This commit is contained in:
parent
29cae03b12
commit
810c8bf275
|
@ -58,14 +58,10 @@ def decode_unified(addr_str):
|
||||||
result = {}
|
result = {}
|
||||||
while len(rest) > 0:
|
while len(rest) > 0:
|
||||||
if s == 0:
|
if s == 0:
|
||||||
(receiver_type, receiver_type_len) = parse_compact_size(rest)
|
(receiver_type, rest) = parse_compact_size(rest)
|
||||||
rest = rest[receiver_type_len:]
|
|
||||||
|
|
||||||
s = 1
|
s = 1
|
||||||
elif s == 1:
|
elif s == 1:
|
||||||
(receiver_len, receiver_len_len) = parse_compact_size(rest)
|
(receiver_len, rest) = parse_compact_size(rest)
|
||||||
rest = rest[receiver_len_len:]
|
|
||||||
|
|
||||||
expected_len = {0: 20, 1: 20, 2: 43, 3: 43}.get(receiver_type)
|
expected_len = {0: 20, 1: 20, 2: 43, 3: 43}.get(receiver_type)
|
||||||
if expected_len is not None:
|
if expected_len is not None:
|
||||||
assert receiver_len == expected_len, "incorrect receiver length"
|
assert receiver_len == expected_len, "incorrect receiver length"
|
||||||
|
|
12
zc_utils.py
12
zc_utils.py
|
@ -14,22 +14,22 @@ def parse_compact_size(rest):
|
||||||
assert len(rest) >= 1
|
assert len(rest) >= 1
|
||||||
b = rest[0]
|
b = rest[0]
|
||||||
if b < 253:
|
if b < 253:
|
||||||
return (b, 1)
|
return (b, rest[1:])
|
||||||
elif b == 253:
|
elif b == 253:
|
||||||
assert len(rest) >= 3
|
assert len(rest) >= 3
|
||||||
return (struct.unpack('<H', rest[1:3])[0], 3)
|
return (struct.unpack('<H', rest[1:3])[0], rest[3:])
|
||||||
elif b == 254:
|
elif b == 254:
|
||||||
assert len(rest) >= 5
|
assert len(rest) >= 5
|
||||||
return (struct.unpack('<I', rest[1:5])[0], 5)
|
return (struct.unpack('<I', rest[1:5])[0], rest[5:])
|
||||||
else:
|
else:
|
||||||
assert len(rest) >= 9
|
assert len(rest) >= 9
|
||||||
return (struct.unpack('<Q', rest[1:9])[0], 9)
|
return (struct.unpack('<Q', rest[1:9])[0], rest[9:])
|
||||||
|
|
||||||
|
|
||||||
def test_round_trip(n, encoding):
|
def test_round_trip(n, encoding):
|
||||||
assert write_compact_size(n) == encoding
|
assert write_compact_size(n) == encoding
|
||||||
assert parse_compact_size(encoding) == (n, len(encoding))
|
assert parse_compact_size(encoding) == (n, b'')
|
||||||
assert parse_compact_size(encoding + b'\x2a') == (n, len(encoding))
|
assert parse_compact_size(encoding + b'*') == (n, b'*')
|
||||||
try:
|
try:
|
||||||
parse_compact_size(encoding[:-1])
|
parse_compact_size(encoding[:-1])
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
|
|
Loading…
Reference in New Issue