From: Brian Warner Date: Fri, 2 Nov 2007 07:03:28 +0000 (-0700) Subject: mutable.py: sketch out data-structure packing/unpacking methods X-Git-Tag: allmydata-tahoe-0.7.0~296 X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg?a=commitdiff_plain;h=6510510ea79bf4981ebbd314a301144fda7f2e9f;p=tahoe-lafs%2Ftahoe-lafs.git mutable.py: sketch out data-structure packing/unpacking methods --- diff --git a/src/allmydata/mutable.py b/src/allmydata/mutable.py index b4b86035..1d877740 100644 --- a/src/allmydata/mutable.py +++ b/src/allmydata/mutable.py @@ -1,4 +1,5 @@ +import struct from zope.interface import implements from twisted.internet import defer from allmydata.interfaces import IMutableFileNode, IMutableFileURI @@ -68,4 +69,62 @@ class MutableFileNode: def replace(self, newdata): return defer.succeed(None) + def unpack_data(self, data): + offsets = {} + (version, + seqnum, + root_hash, + k, N, segsize, datalen, + offsets['signature'], + offsets['share_hash_chain'], + offsets['block_hash_tree'], + offsets['IV'], + offsets['share_data'], + offsets['enc_privkey']) = struct.unpack(">BQ32s" + "BBQQ" + "LLLLLQ") + assert version == 0 + signature = data[offsets['signature']:offsets['share_hash_chain']] + share_hash_chain = data[offsets['share_hash_chain']:offsets['block_hash_tree']] + block_hash_tree = data[offsets['block_hash_tree']:offsets['IV']] + IV = data[offsets['IV']:offsets['share_data']] + share_data = data[offsets['share_data']:offsets['share_data']+datalen] + enc_privkey = data[offsets['enc_privkey']:] + + def pack_data(self): + # dummy values to satisfy pyflakes until we wire this all up + seqnum, root_hash, k, N, segsize, datalen = 0,0,0,0,0,0 + (verification_key, signature, share_hash_chain, block_hash_tree, + IV, share_data, enc_privkey) = ["0"*16] * 7 + seqnum += 1 + newbuf = [struct.pack(">BQ32s" + "BBQQ", + 0, # version byte + seqnum, + root_hash, + k, N, segsize, datalen)] + post_offset = struct.calcsize(">BQ32s" + "BBQQ" + "LLLLLQ") + offsets = {} + o1 = offsets['signature'] = post_offset + len(verification_key) + o2 = offsets['share_hash_chain'] = o1 + len(signature) + o3 = offsets['block_hash_tree'] = o2 + len(share_hash_chain) + assert len(IV) == 16 + o4 = offsets['IV'] = o3 + len(block_hash_tree) + o5 = offsets['share_data'] = o4 + len(IV) + o6 = offsets['enc_privkey'] = o5 + len(share_data) + + newbuf.append(struct.pack(">LLLLLQ", + offsets['signature'], + offsets['share_hash_chain'], + offsets['block_hash_tree'], + offsets['IV'], + offsets['share_data'], + offsets['enc_privkey'])) + newbuf.extend([verification_key, + signature, + share_hash_chain, + block_hash_tree, + IV, + share_data, + enc_privkey]) + return "".join(newbuf) + + # use client.create_mutable_file() to make one of these