From: Brian Warner Date: Fri, 26 Sep 2008 04:38:24 +0000 (-0700) Subject: move netstring() and split_netstring() into a separate util.netstring module X-Git-Url: https://git.rkrishnan.org/pf/content/en/footer/index.html?a=commitdiff_plain;h=e8cf581e3f9141a1d2ef4c8fddb9f5f3b2066f41;p=tahoe-lafs%2Ftahoe-lafs.git move netstring() and split_netstring() into a separate util.netstring module --- diff --git a/src/allmydata/dirnode.py b/src/allmydata/dirnode.py index 6945bdec..58245100 100644 --- a/src/allmydata/dirnode.py +++ b/src/allmydata/dirnode.py @@ -14,34 +14,10 @@ from allmydata.checker_results import DeepCheckResults, \ from allmydata.util import hashutil, mathutil, base32, log from allmydata.util.hashutil import netstring from allmydata.util.limiter import ConcurrencyLimiter +from allmydata.util.netstring import split_netstring from allmydata.uri import NewDirectoryURI 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: def __init__(self, node, name, must_exist=True): self.node = node diff --git a/src/allmydata/util/hashutil.py b/src/allmydata/util/hashutil.py index 4e762ecf..750543c7 100644 --- a/src/allmydata/util/hashutil.py +++ b/src/allmydata/util/hashutil.py @@ -1,5 +1,6 @@ from pycryptopp.hash.sha256 import SHA256 import os +from allmydata.util.netstring import netstring # Be very very cautious when modifying this file. Almost any change will # cause a compatibility break, invalidating all outstanding URIs and making @@ -15,10 +16,6 @@ CRYPTO_VAL_SIZE=32 class IntegrityCheckError(Exception): pass -def netstring(s): - assert isinstance(s, str), s # no unicode here - return "%d:%s," % (len(s), s,) - class _SHA256d_Hasher: # use SHA-256d, as defined by Ferguson and Schneier: hash the output # again to prevent length-extension attacks diff --git a/src/allmydata/util/netstring.py b/src/allmydata/util/netstring.py new file mode 100644 index 00000000..70a14e01 --- /dev/null +++ b/src/allmydata/util/netstring.py @@ -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) +