changeset 126:ff6f869273b7

Fix wrong init value for computing checksum
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 08 Aug 2011 17:26:05 +0800
parents b9688a3badaa
children 650244dcdd82
files examples/inject_redir.py paraspace/dex_deptracker.py paraspace/dexfile.py paraspace/tests/dex_deptracker_test.py paraspace/tests/dexfile_test.py paraspace/tools.py
diffstat 6 files changed, 44 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/examples/inject_redir.py	Sun Aug 07 23:02:08 2011 +0800
+++ b/examples/inject_redir.py	Mon Aug 08 17:26:05 2011 +0800
@@ -52,7 +52,6 @@
                        excludes=injected_typeidxs)
 
 restore_dependencies(dst_linked, decls)
-dst_linked.make_checksum()
 
 restore_raw = dst_linked.to_str()
 
--- a/paraspace/dex_deptracker.py	Sun Aug 07 23:02:08 2011 +0800
+++ b/paraspace/dex_deptracker.py	Mon Aug 08 17:26:05 2011 +0800
@@ -939,6 +939,9 @@
     
     _sync_dex_maps(dex)
     _sync_dex_header(dex)
+    
+    dex.make_signature()
+    dex.make_checksum()
     pass
 
 
--- a/paraspace/dexfile.py	Sun Aug 07 23:02:08 2011 +0800
+++ b/paraspace/dexfile.py	Mon Aug 08 17:26:05 2011 +0800
@@ -1734,14 +1734,32 @@
         children = map_items
         return children
 
+    ## \biref Make SHA1 signature for DEX.
+    #
+    # This method must be called before make_checksum().
+    #
+    def make_signature(self):
+        from hashlib import sha1
+
+        raw = self.to_str()
+        sz = self.header.fileSize
+        nosum = _DEX_header.magic.sizeof(self.header.magic) + \
+            _DEX_header.checksum.sizeof(self.header.checksum) + \
+            _DEX_header.signature.sizeof(self.header.signature)
+        sha = sha1()
+        sha.update(raw[nosum:])
+        signature = sha.digest()
+        self.header.signature.data = signature
+        pass
+
     def make_checksum(self):
-        from paraspace.tools import adler32
+        from paraspace.tools import adler32, adler32_init_value
         
         raw = self.to_str()
         sz = self.header.fileSize
         nosum = _DEX_header.magic.sizeof(self.header.magic) + \
             _DEX_header.checksum.sizeof(self.header.checksum)
-        checksum = adler32(0, raw, nosum, sz - nosum)
+        checksum = adler32(adler32_init_value, raw, nosum, sz - nosum)
         self.header.checksum = checksum
         pass
     pass
--- a/paraspace/tests/dex_deptracker_test.py	Sun Aug 07 23:02:08 2011 +0800
+++ b/paraspace/tests/dex_deptracker_test.py	Mon Aug 08 17:26:05 2011 +0800
@@ -50,11 +50,21 @@
     dex.typeLists.items[0].value.data_offset = 0
     offset1 = dex.typeLists.items[1].value.data_offset
     dex.typeLists.items[1].value.data_offset = 0
+
+    cdoffset0 = dex.classDatas.items[0].data_offset
+    dex.classDatas.items[0].data_offset = 0
+    cdoffset1 = dex.classDatas.items[1].data_offset
+    dex.classDatas.items[1].data_offset = 0
+    cdoffsetlast = dex.classDatas.items[-1].data_offset
+    dex.classDatas.items[-1].data_offset = 0
     
     update_offset(dex, all_dep_decls)
 
     assert dex.typeLists.items[0].value.data_offset == offset0
     assert dex.typeLists.items[1].value.data_offset == offset1
+    assert dex.classDatas.items[0].data_offset == cdoffset0
+    assert dex.classDatas.items[1].data_offset == cdoffset1
+    assert dex.classDatas.items[-1].data_offset == cdoffsetlast
     pass
 
 
--- a/paraspace/tests/dexfile_test.py	Sun Aug 07 23:02:08 2011 +0800
+++ b/paraspace/tests/dexfile_test.py	Mon Aug 08 17:26:05 2011 +0800
@@ -336,7 +336,7 @@
 
 
 def header_checksum_test():
-    from paraspace.dex_deptracker import collect_all_dep_decls
+    from paraspace.dex_deptracker import collect_all_dep_decls, update_offset
 
     _install_dexfile_4_deptracker()
 
@@ -344,13 +344,17 @@
 
     srcdir = os.path.dirname(__file__)
     srcroot = os.path.join(srcdir, '..', '..')
-    testdatapath = os.path.join(srcroot, 'data', 'testdata1.dex')
+    testdatapath = os.path.join(srcroot, 'data', 'helloworld.dex')
     dex = dexfile.DEXFile.open(testdatapath)
-
+    dex.maps.padding = 1
+    
+    saved_signature = dex.header.signature.data
+    dex.make_signature()
+    assert dex.header.signature.data == saved_signature
+    
     saved_checksum = dex.header.checksum
     dex.make_checksum()
-    # assert dex.header.checksum == saved_checksum
-    assert dex.header.checksum == 0xc5efe529
+    assert dex.header.checksum == saved_checksum
     pass
 
 
--- a/paraspace/tools.py	Sun Aug 07 23:02:08 2011 +0800
+++ b/paraspace/tools.py	Mon Aug 08 17:26:05 2011 +0800
@@ -1,5 +1,7 @@
 base = 65521
 
+adler32_init_value = 1
+
 def adler32(adler, buf, offset, sz):
     sum1 = adler & 0xffff
     sum2 = (adler >> 16) & 0xffff