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:
|
2023-02-03 13:39:31 +00:00
|
|
|
data = r.read_memoryview(2)
|
|
|
|
return int.from_bytes(data, "big")
|
2021-02-05 18:51:01 +00:00
|
|
|
|
|
|
|
|
2021-01-14 18:58:18 +00:00
|
|
|
def read_uint32_be(r: BufferReader) -> int:
|
2023-02-03 13:39:31 +00:00
|
|
|
data = r.read_memoryview(4)
|
|
|
|
return int.from_bytes(data, "big")
|
2021-02-05 18:51:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def read_uint64_be(r: BufferReader) -> int:
|
2023-02-03 13:39:31 +00:00
|
|
|
data = r.read_memoryview(8)
|
|
|
|
return int.from_bytes(data, "big")
|
|
|
|
|
|
|
|
|
|
|
|
def read_uint16_le(r: BufferReader) -> int:
|
|
|
|
data = r.read_memoryview(2)
|
|
|
|
return int.from_bytes(data, "little")
|
|
|
|
|
|
|
|
|
|
|
|
def read_uint32_le(r: BufferReader) -> int:
|
|
|
|
data = r.read_memoryview(4)
|
|
|
|
return int.from_bytes(data, "little")
|
2023-10-18 10:02:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def read_uint64_le(r: BufferReader) -> int:
|
|
|
|
data = r.read_memoryview(8)
|
|
|
|
return int.from_bytes(data, "little")
|