-
Notifications
You must be signed in to change notification settings - Fork 1
/
verify.py
64 lines (62 loc) · 2.58 KB
/
verify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# from sre_parse import State
import struct
from collections import namedtuple
from hashlib import *
from os.path import exists
def verify(path):
FORMAT_HEADER = struct.Struct("20s d 16s I 11s I")
TUPLE_FOR_HEADER = namedtuple("header", "sha1 timestamp case_id item_id state length")
print("Verifying!!")
ids = {}
hashes = []
success = True
count = 0
last_hash = None
with open(path, "rb") as f:
while True:
try:
header_bytes = f.read(68)
header = TUPLE_FOR_HEADER._make(FORMAT_HEADER.unpack_from(header_bytes))
data = f.read(header.length)
state = header.state.decode('utf-8').rstrip('\x00')
if state not in ["CHECKEDIN", "CHECKEDOUT", "DESTROYED", "DISPOSED", "RELEASED", "INITIAL"]:
success = False
exit(1)
if state == "INITIAL" and data.decode('utf-8').rstrip('\x00') != "Initial block":
success = False
exit(1)
if state == "RELEASED" and header.length == 0:
success = False
exit(1)
if (last_hash != None) and (header.sha1 != last_hash):
success = False
exit(1)
if header.item_id in ids:
if state == "CHECKEDIN" and ids[header.item_id] == "CHECKEDIN":
success = False
exit(1)
if state == "CHECKEDOUT" and ids[header.item_id] == "CHECKEDOUT":
success = False
exit(1)
if state in ["DESTROYED", "DISPOSED", "RELEASED"] and ids[header.item_id] in ["DESTROYED", "DISPOSED", "RELEASED"]:
success = False
exit(1)
if state in ["CHECKEDIN", "CHECKEDOUT"] and ids[header.item_id] in ["DESTROYED", "DISPOSED", "RELEASED"]:
success = False
exit(1)
else:
if state in ["DESTROYED", "DISPOSED", "RELEASED"]:
success = False
exit(1)
count = 1
ids[header.item_id] = state
hashes.append(header.sha1)
last_hash = sha1(header_bytes + data).digest()
except:
if success == False:
exit(1)
if count == 0:
exit(1)
if header_bytes:
exit(1)
break