2022-09-19 09:17:36 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from trezor.utils import BufferReader
|
2020-06-24 12:25:48 +00:00
|
|
|
|
|
|
|
|
2022-01-17 12:03:21 +00:00
|
|
|
def read_compact_size(r: BufferReader) -> int:
|
2022-09-19 09:17:36 +00:00
|
|
|
get = r.get # local_cache_attribute
|
|
|
|
|
|
|
|
prefix = get()
|
2020-06-24 12:25:48 +00:00
|
|
|
if prefix < 253:
|
|
|
|
n = prefix
|
|
|
|
elif prefix == 253:
|
2022-09-19 09:17:36 +00:00
|
|
|
n = get()
|
|
|
|
n += get() << 8
|
2020-06-24 12:25:48 +00:00
|
|
|
elif prefix == 254:
|
2022-09-19 09:17:36 +00:00
|
|
|
n = get()
|
|
|
|
n += get() << 8
|
|
|
|
n += get() << 16
|
|
|
|
n += get() << 24
|
2022-01-17 12:03:21 +00:00
|
|
|
elif prefix == 255:
|
2022-09-19 09:17:36 +00:00
|
|
|
n = get()
|
|
|
|
n += get() << 8
|
|
|
|
n += get() << 16
|
|
|
|
n += get() << 24
|
|
|
|
n += get() << 32
|
|
|
|
n += get() << 40
|
|
|
|
n += get() << 48
|
|
|
|
n += get() << 56
|
2020-06-24 12:25:48 +00:00
|
|
|
else:
|
|
|
|
raise ValueError
|
|
|
|
return n
|
2021-01-14 18:58:18 +00:00
|
|
|
|
|
|
|
|
2021-02-05 18:51:01 +00:00
|
|
|
def read_uint16_be(r: BufferReader) -> int:
|
|
|
|
n = r.get()
|
|
|
|
return (n << 8) + r.get()
|
|
|
|
|
|
|
|
|
2021-01-14 18:58:18 +00:00
|
|
|
def read_uint32_be(r: BufferReader) -> int:
|
2021-02-05 18:51:01 +00:00
|
|
|
n = r.get()
|
|
|
|
for _ in range(3):
|
|
|
|
n = (n << 8) + r.get()
|
|
|
|
return n
|
|
|
|
|
|
|
|
|
|
|
|
def read_uint64_be(r: BufferReader) -> int:
|
|
|
|
n = r.get()
|
|
|
|
for _ in range(7):
|
|
|
|
n = (n << 8) + r.get()
|
2021-01-14 18:58:18 +00:00
|
|
|
return n
|