We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0d7f18e commit 09ba792Copy full SHA for 09ba792
src/sfnttools/internal/checksum.py
@@ -0,0 +1,14 @@
1
+from typing import Final
2
+
3
+CHECKSUM_MASK: Final = 0xFFFFFFFF
4
5
6
+def calculate_checksum(data: bytes) -> int:
7
+ checksum = 0
8
+ for i in range(0, len(data), 4):
9
+ chunk = data[i:i + 4]
10
+ if len(chunk) < 4:
11
+ chunk += b'\x00' * (4 - len(chunk))
12
+ checksum += int.from_bytes(chunk, 'big', signed=False)
13
+ checksum &= CHECKSUM_MASK
14
+ return checksum
tests/test_checksum.py
@@ -0,0 +1,6 @@
+from sfnttools.internal.checksum import calculate_checksum
+def test_calculate_checksum():
+ assert calculate_checksum(b'abcd') == 1633837924
+ assert calculate_checksum(b'abcdxyz') == 3655064932
0 commit comments