Changeset efafcfb in trunk
- Timestamp:
- 2009-07-05T02:51:09Z (16 years ago)
- Branches:
- master
- Children:
- c678e8c
- Parents:
- 4206a2c
- Location:
- src/allmydata
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/allmydata/dirnode.py ¶
r4206a2c refafcfb 205 205 # cleartext. The 'name' is UTF-8 encoded. The rwcap is formatted as: 206 206 # pack("16ss32s", iv, AES(H(writekey+iv), plaintextrwcap), mac) 207 assert isinstance(data, str) 207 assert isinstance(data, str), (repr(data), type(data)) 208 208 # an empty directory is serialized as an empty string 209 209 if data == "": … … 211 211 writeable = not self.is_readonly() 212 212 children = {} 213 while len(data) > 0: 214 entry, data = split_netstring(data, 1, True) 215 name, rocap, rwcapdata, metadata_s = split_netstring(entry, 4) 213 position = 0 214 while position < len(data): 215 entries, position = split_netstring(data, 1, position) 216 (name, rocap, rwcapdata, metadata_s), subpos = split_netstring(entries[0], 4) 216 217 name = name.decode("utf-8") 217 218 rwcap = None -
TabularUnified src/allmydata/test/test_netstring.py ¶
r4206a2c refafcfb 6 6 def test_split(self): 7 7 a = netstring("hello") + netstring("world") 8 self.failUnlessEqual(split_netstring(a, 2), ("hello", "world")) 9 self.failUnlessEqual(split_netstring(a, 2, False), ("hello", "world")) 10 self.failUnlessEqual(split_netstring(a, 2, True), 11 ("hello", "world", "")) 8 self.failUnlessEqual(split_netstring(a, 2), (["hello", "world"], len(a))) 9 self.failUnlessEqual(split_netstring(a, 2, required_trailer=""), (["hello", "world"], len(a))) 12 10 self.failUnlessRaises(ValueError, split_netstring, a, 3) 13 self.failUnlessRaises(ValueError, split_netstring, a+" extra", 2 )14 self.failUnless Raises(ValueError, split_netstring, a+" extra", 2, False)11 self.failUnlessRaises(ValueError, split_netstring, a+" extra", 2, required_trailer="") 12 self.failUnlessEqual(split_netstring(a+" extra", 2), (["hello", "world"], len(a))) 15 13 self.failUnlessEqual(split_netstring(a+"++", 2, required_trailer="++"), 16 ( "hello", "world"))14 (["hello", "world"], len(a)+2)) 17 15 self.failUnlessRaises(ValueError, 18 16 split_netstring, a+"+", 2, required_trailer="not") … … 20 18 def test_extra(self): 21 19 a = netstring("hello") 22 self.failUnlessEqual(split_netstring(a, 1 , True), ("hello", ""))20 self.failUnlessEqual(split_netstring(a, 1), (["hello"], len(a))) 23 21 b = netstring("hello") + "extra stuff" 24 self.failUnlessEqual(split_netstring(b, 1 , True),25 ( "hello", "extra stuff"))22 self.failUnlessEqual(split_netstring(b, 1), 23 (["hello"], len(a))) 26 24 27 25 def test_nested(self): 28 26 a = netstring("hello") + netstring("world") + "extra stuff" 29 27 b = netstring("a") + netstring("is") + netstring(a) + netstring(".") 30 top= split_netstring(b, 4)28 (top, pos) = split_netstring(b, 4) 31 29 self.failUnlessEqual(len(top), 4) 32 30 self.failUnlessEqual(top[0], "a") … … 34 32 self.failUnlessEqual(top[2], a) 35 33 self.failUnlessEqual(top[3], ".") 36 self.failUnlessRaises(ValueError, split_netstring, a, 2) 37 self.failUnlessRaises(ValueError, split_netstring, a, 2, False) 38 bottom = split_netstring(a, 2, True) 39 self.failUnlessEqual(bottom, ("hello", "world", "extra stuff")) 40 41 34 self.failUnlessRaises(ValueError, split_netstring, a, 2, required_trailer="") 35 bottom = split_netstring(a, 2) 36 self.failUnlessEqual(bottom, (["hello", "world"], len(netstring("hello")+netstring("world")))) -
TabularUnified src/allmydata/util/netstring.py ¶
r4206a2c refafcfb 6 6 7 7 def split_netstring(data, numstrings, 8 allow_leftover=False,9 required_trailer= ""):10 """like string.split(), but extracts netstrings. I f allow_leftover=False,11 I return numstrings elements, and throw ValueError if there was leftover12 data that does not exactly equal 'required_trailer'. If13 allow_leftover=True, required_trailer must be empty, and I return14 numstrings+1 elements, in which the last element is the leftover data15 (possibly an empty string)"""8 position=0, 9 required_trailer=None): 10 """like string.split(), but extracts netstrings. Ignore all bytes of data 11 before the 'position' byte. Return a tuple of (list of elements (numstrings 12 in length), new position index). The new position index points to the first 13 byte which was not consumed (the 'required_trailer', if any, counts as 14 consumed). If 'required_trailer' is not None, throw ValueError if leftover 15 data does not exactly equal 'required_trailer'.""" 16 16 17 assert not (allow_leftover and required_trailer) 18 17 assert type(position) in (int, long), (repr(position), type(position)) 19 18 elements = [] 20 19 assert numstrings >= 0 21 while data:22 colon = data.index(":" )23 length = int(data[ :colon])20 while position < len(data): 21 colon = data.index(":", position) 22 length = int(data[position:colon]) 24 23 string = data[colon+1:colon+1+length] 25 assert len(string) == length 24 assert len(string) == length, (len(string), length) 26 25 elements.append(string) 27 assert data[colon+1+length] == "," 28 data = data[colon+1+length+1:] 26 position = colon+1+length 27 assert data[position] == ",", position 28 position += 1 29 29 if len(elements) == numstrings: 30 30 break 31 31 if len(elements) < numstrings: 32 32 raise ValueError("ran out of netstrings") 33 if allow_leftover:34 return tuple(elements + [data])35 if data != required_trailer:36 r aise ValueError("leftover data in netstrings")37 return tuple(elements)38 33 if required_trailer is not None: 34 if ((len(data) - position) != len(required_trailer)) or (data[position:] != required_trailer): 35 raise ValueError("leftover data in netstrings") 36 return (elements, position + len(required_trailer)) 37 else: 38 return (elements, position)
Note: See TracChangeset
for help on using the changeset viewer.