Skip to content

Commit

Permalink
[tls] use int.from_bytes and int.to_bytes
Browse files Browse the repository at this point in the history
The standard library provides us with methods for reading and writing
big-endian integers, use them.
  • Loading branch information
jlaine committed Jul 17, 2022
1 parent 7b3f24b commit 9918382
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/aioquic/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,7 @@ class SignatureAlgorithm(IntEnum):

@contextmanager
def pull_block(buf: Buffer, capacity: int) -> Generator:
length = 0
for b in buf.pull_bytes(capacity):
length = (length << 8) | b
length = int.from_bytes(buf.pull_bytes(capacity), byteorder="big")
end = buf.tell() + length
yield length
assert buf.tell() == end
Expand All @@ -361,10 +359,8 @@ def push_block(buf: Buffer, capacity: int) -> Generator:
yield
end = buf.tell()
length = end - start
while capacity:
buf.seek(start - capacity)
buf.push_uint8((length >> (8 * (capacity - 1))) & 0xFF)
capacity -= 1
buf.seek(start - capacity)
buf.push_bytes(length.to_bytes(capacity, byteorder="big"))
buf.seek(end)


Expand Down Expand Up @@ -1218,10 +1214,9 @@ def handle_message(
while len(self._receive_buffer) >= 4:
# determine message length
message_type = self._receive_buffer[0]
message_length = 0
for b in self._receive_buffer[1:4]:
message_length = (message_length << 8) | b
message_length += 4
message_length = 4 + int.from_bytes(
self._receive_buffer[1:4], byteorder="big"
)

# check message is complete
if len(self._receive_buffer) < message_length:
Expand Down

0 comments on commit 9918382

Please sign in to comment.