move netstring() and split_netstring() into a separate util.netstring module
This commit is contained in:
parent
39fa9018ef
commit
e8cf581e3f
|
@ -14,34 +14,10 @@ from allmydata.checker_results import DeepCheckResults, \
|
||||||
from allmydata.util import hashutil, mathutil, base32, log
|
from allmydata.util import hashutil, mathutil, base32, log
|
||||||
from allmydata.util.hashutil import netstring
|
from allmydata.util.hashutil import netstring
|
||||||
from allmydata.util.limiter import ConcurrencyLimiter
|
from allmydata.util.limiter import ConcurrencyLimiter
|
||||||
|
from allmydata.util.netstring import split_netstring
|
||||||
from allmydata.uri import NewDirectoryURI
|
from allmydata.uri import NewDirectoryURI
|
||||||
from pycryptopp.cipher.aes import AES
|
from pycryptopp.cipher.aes import AES
|
||||||
|
|
||||||
def split_netstring(data, numstrings, allow_leftover=False):
|
|
||||||
"""like string.split(), but extracts netstrings. If allow_leftover=False,
|
|
||||||
returns numstrings elements, and throws ValueError if there was leftover
|
|
||||||
data. If allow_leftover=True, returns numstrings+1 elements, in which the
|
|
||||||
last element is the leftover data (possibly an empty string)"""
|
|
||||||
elements = []
|
|
||||||
assert numstrings >= 0
|
|
||||||
while data:
|
|
||||||
colon = data.index(":")
|
|
||||||
length = int(data[:colon])
|
|
||||||
string = data[colon+1:colon+1+length]
|
|
||||||
assert len(string) == length
|
|
||||||
elements.append(string)
|
|
||||||
assert data[colon+1+length] == ","
|
|
||||||
data = data[colon+1+length+1:]
|
|
||||||
if len(elements) == numstrings:
|
|
||||||
break
|
|
||||||
if len(elements) < numstrings:
|
|
||||||
raise ValueError("ran out of netstrings")
|
|
||||||
if allow_leftover:
|
|
||||||
return tuple(elements + [data])
|
|
||||||
if data:
|
|
||||||
raise ValueError("leftover data in netstrings")
|
|
||||||
return tuple(elements)
|
|
||||||
|
|
||||||
class Deleter:
|
class Deleter:
|
||||||
def __init__(self, node, name, must_exist=True):
|
def __init__(self, node, name, must_exist=True):
|
||||||
self.node = node
|
self.node = node
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from pycryptopp.hash.sha256 import SHA256
|
from pycryptopp.hash.sha256 import SHA256
|
||||||
import os
|
import os
|
||||||
|
from allmydata.util.netstring import netstring
|
||||||
|
|
||||||
# Be very very cautious when modifying this file. Almost any change will
|
# Be very very cautious when modifying this file. Almost any change will
|
||||||
# cause a compatibility break, invalidating all outstanding URIs and making
|
# cause a compatibility break, invalidating all outstanding URIs and making
|
||||||
|
@ -15,10 +16,6 @@ CRYPTO_VAL_SIZE=32
|
||||||
class IntegrityCheckError(Exception):
|
class IntegrityCheckError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def netstring(s):
|
|
||||||
assert isinstance(s, str), s # no unicode here
|
|
||||||
return "%d:%s," % (len(s), s,)
|
|
||||||
|
|
||||||
class _SHA256d_Hasher:
|
class _SHA256d_Hasher:
|
||||||
# use SHA-256d, as defined by Ferguson and Schneier: hash the output
|
# use SHA-256d, as defined by Ferguson and Schneier: hash the output
|
||||||
# again to prevent length-extension attacks
|
# again to prevent length-extension attacks
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
|
||||||
|
|
||||||
|
def netstring(s):
|
||||||
|
assert isinstance(s, str), s # no unicode here
|
||||||
|
return "%d:%s," % (len(s), s,)
|
||||||
|
|
||||||
|
def split_netstring(data, numstrings, allow_leftover=False):
|
||||||
|
"""like string.split(), but extracts netstrings. If allow_leftover=False,
|
||||||
|
returns numstrings elements, and throws ValueError if there was leftover
|
||||||
|
data. If allow_leftover=True, returns numstrings+1 elements, in which the
|
||||||
|
last element is the leftover data (possibly an empty string)"""
|
||||||
|
elements = []
|
||||||
|
assert numstrings >= 0
|
||||||
|
while data:
|
||||||
|
colon = data.index(":")
|
||||||
|
length = int(data[:colon])
|
||||||
|
string = data[colon+1:colon+1+length]
|
||||||
|
assert len(string) == length
|
||||||
|
elements.append(string)
|
||||||
|
assert data[colon+1+length] == ","
|
||||||
|
data = data[colon+1+length+1:]
|
||||||
|
if len(elements) == numstrings:
|
||||||
|
break
|
||||||
|
if len(elements) < numstrings:
|
||||||
|
raise ValueError("ran out of netstrings")
|
||||||
|
if allow_leftover:
|
||||||
|
return tuple(elements + [data])
|
||||||
|
if data:
|
||||||
|
raise ValueError("leftover data in netstrings")
|
||||||
|
return tuple(elements)
|
||||||
|
|
Loading…
Reference in New Issue