]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
mutable.py: sketch out data-structure packing/unpacking methods
authorBrian Warner <warner@lothar.com>
Fri, 2 Nov 2007 07:03:28 +0000 (00:03 -0700)
committerBrian Warner <warner@lothar.com>
Fri, 2 Nov 2007 07:03:28 +0000 (00:03 -0700)
src/allmydata/mutable.py

index b4b86035d55bd8a894487bc66deb845bcae39aa6..1d87774078f105e012d6c791eb6f9d11cae9da81 100644 (file)
@@ -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