Skip to content

Commit 1884cf1

Browse files
committed
Centralize adding and removing of varint length in codec.py
1 parent 500b476 commit 1884cf1

File tree

5 files changed

+20
-36
lines changed

5 files changed

+20
-36
lines changed

multiaddr/codec.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ def string_to_bytes(string):
3737
for proto, codec, value in string_iter(string):
3838
bs.append(varint.encode(proto.code))
3939
if value is not None:
40-
bs.append(codec.to_bytes(proto, value))
40+
buf = codec.to_bytes(proto, value)
41+
if codec.SIZE == LENGTH_PREFIXED_VAR_SIZE:
42+
bs.append(varint.encode(len(buf)))
43+
bs.append(buf)
4144
return b''.join(bs)
4245

4346

@@ -56,10 +59,9 @@ def bytes_to_string(buf):
5659

5760
def size_for_addr(codec, buf):
5861
if codec.SIZE >= 0:
59-
return codec.SIZE // 8
62+
return codec.SIZE // 8, 0
6063
else:
61-
size, num_bytes_read = read_varint_code(buf)
62-
return size + num_bytes_read
64+
return read_varint_code(buf)
6365

6466

6567
def string_iter(string):
@@ -102,7 +104,7 @@ def bytes_iter(buf):
102104
codec = find_codec_by_name(proto.codec)
103105
except ImportError as exc:
104106
six.raise_from(ValueError("failed to parse %s addr: unknown" % proto.name), exc)
105-
size = size_for_addr(codec, buf[num_bytes_read:])
106-
length = size + num_bytes_read
107-
yield proto, codec, buf[num_bytes_read:length]
107+
size, num_bytes_read2 = size_for_addr(codec, buf[num_bytes_read:])
108+
length = size + num_bytes_read2 + num_bytes_read
109+
yield proto, codec, buf[(length - size):length]
108110
buf = buf[length:]

multiaddr/codecs/fspath.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
import os
33

44
import six
5-
import varint
65

76
from ..codec import LENGTH_PREFIXED_VAR_SIZE
8-
from ..protocols import read_varint_code
97

108

119
SIZE = LENGTH_PREFIXED_VAR_SIZE
@@ -30,11 +28,8 @@ def fsdecode(path):
3028

3129

3230
def to_bytes(proto, string):
33-
bytes = fsencode(string)
34-
size = varint.encode(len(bytes))
35-
return b''.join([size, bytes])
31+
return fsencode(string)
3632

3733

3834
def to_string(proto, buf):
39-
size, num_bytes_read = read_varint_code(buf)
40-
return fsdecode(buf[num_bytes_read:])
35+
return fsdecode(buf)

multiaddr/codecs/idna.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
from __future__ import absolute_import
22

33
import idna
4-
import varint
54

65
from ..codec import LENGTH_PREFIXED_VAR_SIZE
7-
from ..protocols import read_varint_code
86

97

108
SIZE = LENGTH_PREFIXED_VAR_SIZE
119
IS_PATH = False
1210

1311

1412
def to_bytes(proto, string):
15-
bytes = idna.encode(string, uts46=True)
16-
size = varint.encode(len(bytes))
17-
return b''.join([size, bytes])
13+
return idna.encode(string, uts46=True)
1814

1915

2016
def to_string(proto, buf):
21-
size, num_bytes_read = read_varint_code(buf)
22-
return idna.decode(buf[num_bytes_read:])
17+
return idna.decode(buf)

multiaddr/codecs/p2p.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
import base58
44
import six
5-
import varint
65

76
from ..codec import LENGTH_PREFIXED_VAR_SIZE
8-
from ..protocols import read_varint_code
97

108

119
SIZE = LENGTH_PREFIXED_VAR_SIZE
@@ -20,15 +18,10 @@ def to_bytes(proto, string):
2018
mm = base58.b58decode(string)
2119
except Exception as ex:
2220
raise ValueError("failed to parse p2p addr: %s %s" % (string, str(ex)))
23-
size = varint.encode(len(mm))
2421
if len(mm) < 5:
2522
raise ValueError("invalid P2P multihash: %s" % mm)
26-
return b''.join([size, mm])
23+
return mm
2724

2825

2926
def to_string(proto, buf):
30-
size, num_bytes_read = read_varint_code(buf)
31-
buf = buf[num_bytes_read:]
32-
if len(buf) != size:
33-
raise ValueError("inconsistent lengths")
3427
return base58.b58encode(buf).decode('ascii')

tests/test_codec.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626
b'\x9a\x18\x08\x73\x06\x36\x90\x43\x09\x1f\x04\xd2',
2727
'timaq4ygg2iegci7:1234'),
2828
(_names_to_protocols['p2p'],
29-
b'\x22\x12\x20\xd5\x2e\xbb\x89\xd8\x5b\x02\xa2\x84\x94\x82\x03\xa6\x2f\xf2'
29+
b'\x12\x20\xd5\x2e\xbb\x89\xd8\x5b\x02\xa2\x84\x94\x82\x03\xa6\x2f\xf2'
3030
b'\x83\x89\xc5\x7c\x9f\x42\xbe\xec\x4e\xc2\x0d\xb7\x6a\x68\x91\x1c\x0b',
3131
'QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'),
3232

3333
# Additional test data
3434
(_names_to_protocols['dns4'],
35-
b'\x30xn--4gbrim.xn----ymcbaaajlc6dj7bxne2c.xn--wgbh1c',
35+
b'xn--4gbrim.xn----ymcbaaajlc6dj7bxne2c.xn--wgbh1c',
3636
u'موقع.وزارة-الاتصالات.مصر'), # Explicietly mark this as unicode to force the text to be LTR in editors
3737
(_names_to_protocols['dns4'],
38-
b'\x16xn--fuball-cta.example',
38+
b'xn--fuball-cta.example',
3939
u'fußball.example'), # This will fail if IDNA-2003/NamePrep is used
4040
]
4141

@@ -48,9 +48,9 @@
4848

4949

5050
@pytest.mark.parametrize("codec_name, buf, expected", [
51-
(None, b'\x01\x02\x03', 0),
52-
('ip4', b'\x01\x02\x03', 4),
53-
('p2p', b'\x40\x50\x60\x51', 65),
51+
(None, b'\x01\x02\x03', (0, 0)),
52+
('ip4', b'\x01\x02\x03', (4, 0)),
53+
('p2p', b'\x40\x50\x60\x51', (64, 1)),
5454
])
5555
def test_size_for_addr(codec_name, buf, expected):
5656
assert size_for_addr(find_codec_by_name(codec_name), buf) == expected
@@ -147,7 +147,6 @@ def test_codec_to_bytes_value_error(proto, address):
147147

148148

149149
@pytest.mark.parametrize("proto, buf", [
150-
(_names_to_protocols['p2p'], b'\x15\x23\x0d\x52\xeb\xb8\x9d\x85\xb0\x2a\x28\x49\x48\x20\x3a'),
151150
(_names_to_protocols['tcp'], b'\xff\xff\xff\xff')
152151
])
153152
def test_codec_to_string_value_error(proto, buf):

0 commit comments

Comments
 (0)