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.
This commit is contained in:
Simon Tatham 2017-05-16 20:11:53 +01:00
parent dc057115b5
commit 4aa85aa621

View file

@ -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("<L", data, offset)[0]