Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

Commit 3374d44

Browse files
committed
More robust tar checksum parsing
1 parent 57dcce7 commit 3374d44

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

autoarchaeologist/os/unix/tar_file.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,26 @@ def __init__(self, tree, lo):
5757
if csf[-2] != 0x00:
5858
raise Invalid("Checksum[-2] non-zero")
5959

60+
for i in csf:
61+
if i not in b'01234567 \x00':
62+
raise Invalid("Checksum not valid %s" % str(csf))
63+
64+
csf = bytes(csf[:-2]).lstrip(b'\x20')
65+
try:
66+
rsum = int(csf, 8)
67+
except ValueError:
68+
raise Invalid("Checksum not valid %s" % str(csf))
69+
6070
csum = 0
6171
for i in range(0x200):
6272
if 148 <= i < 156:
6373
csum += 0x20
6474
else:
6575
csum += tree.this[lo + i]
6676

67-
csf = bytes(csf[:-2]).lstrip(b'\x20')
68-
if int(csf, 8) != csum:
77+
if rsum != csum:
6978
raise Invalid(
70-
"Wrong checksum 0x%x != 0x%x (%s)" % (csum, int(csf, 8), str(csf))
79+
"Wrong checksum 0x%x != 0x%x (%s)" % (csum, rsum, str(csf))
7180
)
7281

7382
super().__init__(

0 commit comments

Comments
 (0)