2021-12-08 09:10:58 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2018-10-12 14:50:18 +00:00
|
|
|
from trezor.utils import ensure
|
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
if TYPE_CHECKING:
|
2019-07-03 13:07:04 +00:00
|
|
|
from trezor.utils import Writer
|
|
|
|
|
2018-10-12 14:50:18 +00:00
|
|
|
|
2022-09-19 09:17:36 +00:00
|
|
|
def _write_uint(w: Writer, n: int, bits: int, bigendian: bool) -> int:
|
|
|
|
ensure(0 <= n <= 2**bits - 1, "overflow")
|
|
|
|
shifts = range(0, bits, 8)
|
|
|
|
if bigendian:
|
|
|
|
shifts = reversed(shifts)
|
|
|
|
for num in shifts:
|
|
|
|
w.append((n >> num) & 0xFF)
|
|
|
|
return bits // 8
|
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint8(w: Writer, n: int) -> int:
|
2022-09-19 09:17:36 +00:00
|
|
|
return _write_uint(w, n, 8, False)
|
2018-08-23 12:39:30 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint16_le(w: Writer, n: int) -> int:
|
2022-09-19 09:17:36 +00:00
|
|
|
return _write_uint(w, n, 16, False)
|
2018-10-08 12:34:51 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint32_le(w: Writer, n: int) -> int:
|
2022-09-19 09:17:36 +00:00
|
|
|
return _write_uint(w, n, 32, False)
|
2018-08-23 12:39:30 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint32_be(w: Writer, n: int) -> int:
|
2022-09-19 09:17:36 +00:00
|
|
|
return _write_uint(w, n, 32, True)
|
2018-08-23 12:39:30 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint64_le(w: Writer, n: int) -> int:
|
2022-09-19 09:17:36 +00:00
|
|
|
return _write_uint(w, n, 64, False)
|
2018-08-23 12:39:30 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint64_be(w: Writer, n: int) -> int:
|
2022-09-19 09:17:36 +00:00
|
|
|
return _write_uint(w, n, 64, True)
|
2018-08-23 12:39:30 +00:00
|
|
|
|
|
|
|
|
2022-01-07 14:13:54 +00:00
|
|
|
def write_bytes_unchecked(w: Writer, b: bytes | memoryview) -> int:
|
2018-08-23 12:39:30 +00:00
|
|
|
w.extend(b)
|
|
|
|
return len(b)
|
|
|
|
|
|
|
|
|
2020-03-16 10:31:11 +00:00
|
|
|
def write_bytes_fixed(w: Writer, b: bytes, length: int) -> int:
|
|
|
|
ensure(len(b) == length)
|
|
|
|
w.extend(b)
|
|
|
|
return length
|
|
|
|
|
|
|
|
|
|
|
|
def write_bytes_reversed(w: Writer, b: bytes, length: int) -> int:
|
|
|
|
ensure(len(b) == length)
|
2018-08-23 12:39:30 +00:00
|
|
|
w.extend(bytes(reversed(b)))
|
2020-03-16 10:31:11 +00:00
|
|
|
return length
|
2020-05-15 18:59:06 +00:00
|
|
|
|
|
|
|
|
2022-01-17 12:03:21 +00:00
|
|
|
def write_compact_size(w: Writer, n: int) -> None:
|
2021-10-13 13:45:53 +00:00
|
|
|
ensure(0 <= n <= 0xFFFF_FFFF)
|
2022-09-19 09:17:36 +00:00
|
|
|
append = w.append # local_cache_attribute
|
|
|
|
|
2020-05-15 18:59:06 +00:00
|
|
|
if n < 253:
|
2022-09-19 09:17:36 +00:00
|
|
|
append(n & 0xFF)
|
2020-11-23 13:24:13 +00:00
|
|
|
elif n < 0x1_0000:
|
2022-09-19 09:17:36 +00:00
|
|
|
append(253)
|
2022-01-17 12:03:21 +00:00
|
|
|
write_uint16_le(w, n)
|
|
|
|
elif n < 0x1_0000_0000:
|
2022-09-19 09:17:36 +00:00
|
|
|
append(254)
|
2022-01-17 12:03:21 +00:00
|
|
|
write_uint32_le(w, n)
|
|
|
|
else:
|
2022-09-19 09:17:36 +00:00
|
|
|
append(255)
|
2022-01-17 12:03:21 +00:00
|
|
|
write_uint64_le(w, n)
|
2021-05-27 11:48:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def write_uvarint(w: Writer, n: int) -> None:
|
2021-10-13 13:45:53 +00:00
|
|
|
ensure(0 <= n <= 0xFFFF_FFFF_FFFF_FFFF)
|
2021-05-27 11:48:16 +00:00
|
|
|
shifted = 1
|
|
|
|
while shifted:
|
|
|
|
shifted = n >> 7
|
|
|
|
byte = (n & 0x7F) | (0x80 if shifted else 0x00)
|
|
|
|
w.append(byte)
|
|
|
|
n = shifted
|