Skip to content

Commit

Permalink
add function to convert int to compressed int
Browse files Browse the repository at this point in the history
  • Loading branch information
malwarefrank committed Mar 17, 2024
1 parent 6446867 commit 817f434
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/dnfile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ def read_compressed_int(data) -> Optional[Tuple[int, int]]:
return None


def compress_int(i: int) -> Optional[bytes]:
"""
Given integer, return bytes representing a compressed integer per
spec ECMA-335 II.23.2 Blobs and signatures.
"""

if not isinstance(i, int):
raise TypeError(f"expected int, given {type(i)}")
if i < 0:
raise ValueError(f"expected positive int, given {i}")

if i <= 0x7f:
return int.to_bytes(i, length=1, byteorder="big")
elif i <= 0x3fff:
b = int.to_bytes(i, length=2, byteorder="big")
return bytes((0x80 | b[0], b[1]))
elif i <= 0x1fffffff:
b = int.to_bytes(i, length=4, byteorder="big")
return bytes((0x80 | 0x40 | b[0], b[1], b[2], b[3]))
else:
logger.warning(f"invalid int {i}, max value 0x1fffffff")
return None


def two_way_dict(pairs):
return dict([(e[1], e[0]) for e in pairs] + pairs)

Expand Down

0 comments on commit 817f434

Please sign in to comment.