From 4aa85aa621dd2fd0b33a74ceb6821213846df7da Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Tue, 16 May 2017 20:11:53 +0100 Subject: [PATCH] Fix checksum calculation in CAB data records. [MS-CAB] is pretty unclearly worded in this area. Turns out that if the sequence of bytes being checksummed is not a multiple of 4 bytes, you are supposed to _first_ reverse the order of the trailing (n % 4) bytes, and _then_ append zero bytes to pad to a multiple of 4, before you do the main checksum operation of XORing together all the 32-bit words of the input. --- makecab.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/makecab.py b/makecab.py index befa94a..77c1e4f 100755 --- a/makecab.py +++ b/makecab.py @@ -34,7 +34,11 @@ def packtime(h,m,s): return ((h << 11) | (m << 5) | (s >> 1)) def checksum(data): - data += "\0" * (3 & -len(data)) # pad to multiple of 4 bytes + data_full_words = data[:len(data) & ~3] + data_last_word = data[len(data_full_words):] + data_last_word = "".join(reversed(data_last_word)) + data_last_word += "\0" * (3 & -len(data)) # pad to multiple of 4 bytes + data = data_full_words + data_last_word toret = 0 for offset in xrange(0, len(data), 4): toret ^= struct.unpack_from("