You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.8 KiB
79 lines
1.8 KiB
6 years ago
|
from trezor.utils import ensure
|
||
|
|
||
|
|
||
6 years ago
|
def empty_bytearray(preallocate: int) -> bytearray:
|
||
|
"""
|
||
|
Returns bytearray that won't allocate for at least `preallocate` bytes.
|
||
|
Useful in case you want to avoid allocating too often.
|
||
|
"""
|
||
|
b = bytearray(preallocate)
|
||
|
b[:] = bytes()
|
||
|
return b
|
||
|
|
||
|
|
||
|
def write_uint8(w: bytearray, n: int) -> int:
|
||
6 years ago
|
ensure(0 <= n <= 0xFF)
|
||
6 years ago
|
w.append(n)
|
||
|
return 1
|
||
|
|
||
|
|
||
6 years ago
|
def write_uint16_le(w: bytearray, n: int) -> int:
|
||
6 years ago
|
ensure(0 <= n <= 0xFFFF)
|
||
6 years ago
|
w.append(n & 0xFF)
|
||
|
w.append((n >> 8) & 0xFF)
|
||
|
return 2
|
||
|
|
||
|
|
||
6 years ago
|
def write_uint32_le(w: bytearray, n: int) -> int:
|
||
6 years ago
|
ensure(0 <= n <= 0xFFFFFFFF)
|
||
6 years ago
|
w.append(n & 0xFF)
|
||
|
w.append((n >> 8) & 0xFF)
|
||
|
w.append((n >> 16) & 0xFF)
|
||
|
w.append((n >> 24) & 0xFF)
|
||
|
return 4
|
||
|
|
||
|
|
||
|
def write_uint32_be(w: bytearray, n: int) -> int:
|
||
6 years ago
|
ensure(0 <= n <= 0xFFFFFFFF)
|
||
6 years ago
|
w.append((n >> 24) & 0xFF)
|
||
|
w.append((n >> 16) & 0xFF)
|
||
|
w.append((n >> 8) & 0xFF)
|
||
|
w.append(n & 0xFF)
|
||
|
return 4
|
||
|
|
||
|
|
||
|
def write_uint64_le(w: bytearray, n: int) -> int:
|
||
6 years ago
|
ensure(0 <= n <= 0xFFFFFFFFFFFFFFFF)
|
||
6 years ago
|
w.append(n & 0xFF)
|
||
|
w.append((n >> 8) & 0xFF)
|
||
|
w.append((n >> 16) & 0xFF)
|
||
|
w.append((n >> 24) & 0xFF)
|
||
|
w.append((n >> 32) & 0xFF)
|
||
|
w.append((n >> 40) & 0xFF)
|
||
|
w.append((n >> 48) & 0xFF)
|
||
|
w.append((n >> 56) & 0xFF)
|
||
|
return 8
|
||
|
|
||
|
|
||
|
def write_uint64_be(w: bytearray, n: int) -> int:
|
||
6 years ago
|
ensure(0 <= n <= 0xFFFFFFFFFFFFFFFF)
|
||
6 years ago
|
w.append((n >> 56) & 0xFF)
|
||
|
w.append((n >> 48) & 0xFF)
|
||
|
w.append((n >> 40) & 0xFF)
|
||
|
w.append((n >> 32) & 0xFF)
|
||
|
w.append((n >> 24) & 0xFF)
|
||
|
w.append((n >> 16) & 0xFF)
|
||
|
w.append((n >> 8) & 0xFF)
|
||
|
w.append(n & 0xFF)
|
||
|
return 8
|
||
|
|
||
|
|
||
|
def write_bytes(w: bytearray, b: bytes) -> int:
|
||
|
w.extend(b)
|
||
|
return len(b)
|
||
|
|
||
|
|
||
|
def write_bytes_reversed(w: bytearray, b: bytes) -> int:
|
||
|
w.extend(bytes(reversed(b)))
|
||
|
return len(b)
|