[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [stem/master] Alias for splitting bytes
commit 4d5fe240b2db05ba8b640183bd85ff122fde3e8c
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Wed Jan 17 08:15:47 2018 -0800
Alias for splitting bytes
We do quite a bit of splitting bytes into substrings. Adding a little alias for
it.
---
stem/client/__init__.py | 19 ++++++++++++++++++-
stem/client/cell.py | 10 ++++------
2 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/stem/client/__init__.py b/stem/client/__init__.py
index 3b38f8bb..9ad790a8 100644
--- a/stem/client/__init__.py
+++ b/stem/client/__init__.py
@@ -61,6 +61,19 @@ ADDR_INT = {
}
+def split(content, size):
+ """
+ Simple split of bytes into two substrings.
+
+ :param bytes content: string to split
+ :param int size: index to split the string on
+
+ :returns: two value tuple with the split bytes
+ """
+
+ return content[:size], content[size:]
+
+
class Size(object):
"""
Unsigned `struct.pack format
@@ -107,6 +120,8 @@ class Size(object):
:param bytes content: content to encode
+ :returns: **int** with the unpacked value
+
:raises: **ValueError** if packed data isn't of the right size
"""
@@ -122,6 +137,8 @@ class Size(object):
:param bytes content: content to encode
+ :returns: tuple of the form (unpacked, remainder)
+
:raises: **ValueError** if packed data isn't of the right size
"""
@@ -180,7 +197,7 @@ class Address(collections.namedtuple('Address', ['type', 'type_int', 'value', 'v
# TODO: add support for other address types
- address_bin, content = content[:addr_length], content[addr_length:]
+ address_bin, content = split(content, addr_length)
address = None
if addr_type == AddrType.IPv4 and len(address_bin) == 4:
diff --git a/stem/client/cell.py b/stem/client/cell.py
index bc44298f..332e3f16 100644
--- a/stem/client/cell.py
+++ b/stem/client/cell.py
@@ -44,7 +44,7 @@ import random
import sys
from stem import UNDEFINED
-from stem.client import ZERO, Address, Certificate, Size
+from stem.client import ZERO, Address, Certificate, Size, split
from stem.util import _hash_attr, datetime_to_unix
FIXED_PAYLOAD_LEN = 509
@@ -119,9 +119,7 @@ class Cell(object):
if len(content) < payload_len:
raise ValueError('%s cell should have a payload of %i bytes, but only had %i' % (cls.NAME, payload_len, len(content)))
- payload = content[:payload_len]
- content = content[payload_len:]
-
+ payload, content = split(content, payload_len)
return cls._unpack(payload, link_version, circ_id), content
@classmethod
@@ -516,7 +514,7 @@ class CertsCell(Cell):
if cert_size > len(content):
raise ValueError('CERTS cell should have a certificate with %i bytes, but only had %i remaining' % (cert_size, len(content)))
- cert_bytes, content = content[:cert_size], content[cert_size:]
+ cert_bytes, content = split(content, cert_size)
certs.append(Certificate(cert_type, cert_bytes))
return CertsCell(certs)
@@ -573,7 +571,7 @@ class AuthChallengeCell(Cell):
if len(content) < AUTH_CHALLENGE_SIZE + 2:
raise ValueError('AUTH_CHALLENGE payload should be at least 34 bytes, but was %i' % len(content))
- challenge, content = content[:AUTH_CHALLENGE_SIZE], content[AUTH_CHALLENGE_SIZE:]
+ challenge, content = split(content, AUTH_CHALLENGE_SIZE)
method_count, content = Size.SHORT.pop(content)
if len(content) < method_count * 2:
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits