directories: keep track of your position as you decode netstring after netstring from an input buffer instead of copying the trailing part

This makes decoding linear in the number of netstrings instead of O(N^2).
This commit is contained in:
Zooko O'Whielacronx
2009-07-04 19:51:09 -07:00
parent 4206a2c1c7
commit efafcfb91a
3 changed files with 39 additions and 43 deletions

View File

@ -204,15 +204,16 @@ class NewDirectoryNode:
# rocap, rwcap, metadata), in which the name,rocap,metadata are in
# cleartext. The 'name' is UTF-8 encoded. The rwcap is formatted as:
# pack("16ss32s", iv, AES(H(writekey+iv), plaintextrwcap), mac)
assert isinstance(data, str)
assert isinstance(data, str), (repr(data), type(data))
# an empty directory is serialized as an empty string
if data == "":
return {}
writeable = not self.is_readonly()
children = {}
while len(data) > 0:
entry, data = split_netstring(data, 1, True)
name, rocap, rwcapdata, metadata_s = split_netstring(entry, 4)
position = 0
while position < len(data):
entries, position = split_netstring(data, 1, position)
(name, rocap, rwcapdata, metadata_s), subpos = split_netstring(entries[0], 4)
name = name.decode("utf-8")
rwcap = None
if writeable: