From 5d91b2b59f63a9b3cb739943f2fadf03fef0d33f Mon Sep 17 00:00:00 2001 From: derrod Date: Wed, 6 May 2020 19:49:45 +0200 Subject: [PATCH] [utils] Add FRollingHash implementation Seems to be a variation on CRC-64-ECMA. This python version is of course very slow. That should not be a big issue however, since it is only required for serialising rather small save game data. --- legendary/utils/rolling_hash.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 legendary/utils/rolling_hash.py diff --git a/legendary/utils/rolling_hash.py b/legendary/utils/rolling_hash.py new file mode 100644 index 0000000..eb89034 --- /dev/null +++ b/legendary/utils/rolling_hash.py @@ -0,0 +1,25 @@ +# this is the rolling hash Epic uses, it appears to be a variation on CRC-64-ECMA + +hash_poly = 0xC96C5795D7870F42 +hash_table = [] + + +def _init(): + for i in range(256): + for _ in range(8): + if i & 1: + i >>= 1 + i ^= hash_poly + else: + i >>= 1 + hash_table.append(i) + + +def get_hash(data): + if not hash_table: + _init() + + h = 0 + for i in range(len(data)): + h = ((h << 1 | h >> 63) ^ hash_table[c.data[i]]) & 0xffffffffffffffff + return h