Skip to content

Commit f8eef07

Browse files
committed
INTEGRITY: Add all size variants to scan.dat - size, size-r and size-rd.
1 parent 6837bb3 commit f8eef07

File tree

1 file changed

+49
-40
lines changed

1 file changed

+49
-40
lines changed

compute_hash.py

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def is_appledouble(file_byte_stream):
179179
return True
180180

181181
def macbin_get_resfork_data(file_byte_stream):
182-
""" Returns the resource fork's data section as bytes of a macbinary file as well as its size """
182+
""" Returns the resource fork's data section as bytes, data fork size (size), resource fork size (size-r) and data section of resource fork size (size-rd) of a macbinary file """
183183

184184
if not file_byte_stream:
185185
return file_byte_stream
@@ -189,10 +189,10 @@ def macbin_get_resfork_data(file_byte_stream):
189189
(rsrclen,) = struct.unpack(">I", file_byte_stream[0x57:0x5B])
190190

191191
resoure_fork_offset = 128 + datalen_padded
192-
data_offset = int.from_bytes(file_byte_stream[resoure_fork_offset+0 : resoure_fork_offset+4])
193-
data_length = int.from_bytes(file_byte_stream[resoure_fork_offset+8 : resoure_fork_offset+12])
192+
rd_offset = int.from_bytes(file_byte_stream[resoure_fork_offset+0 : resoure_fork_offset+4])
193+
rd_length = int.from_bytes(file_byte_stream[resoure_fork_offset+8 : resoure_fork_offset+12])
194194

195-
return (file_byte_stream[resoure_fork_offset + data_offset: resoure_fork_offset + data_offset + data_length], data_length)
195+
return (file_byte_stream[resoure_fork_offset + rd_offset: resoure_fork_offset + rd_offset + rd_length], datalen, rsrclen, rd_length)
196196

197197
def macbin_get_datafork(file_byte_stream):
198198
if not file_byte_stream:
@@ -222,7 +222,7 @@ def is_appledouble(file_byte_stream):
222222
return True
223223

224224
def appledouble_get_resfork_data(file_byte_stream):
225-
""" Returns the resource fork's data section as bytes of an appledouble file as well as its size """
225+
""" Returns the resource fork's data section as bytes, size of resource fork (size-r) and size of data section of resource fork (size-rd) of an appledouble file"""
226226

227227
entry_count = read_be_16(file_byte_stream[24:])
228228
for entry in range(entry_count):
@@ -233,13 +233,13 @@ def appledouble_get_resfork_data(file_byte_stream):
233233

234234
if id == 2:
235235
resource_fork_stream = file_byte_stream[offset:offset+length]
236-
data_offset = int.from_bytes(resource_fork_stream[0:4])
237-
data_length = int.from_bytes(resource_fork_stream[8:12])
236+
rd_offset = int.from_bytes(resource_fork_stream[0:4])
237+
rd_length = int.from_bytes(resource_fork_stream[8:12])
238238

239-
return (resource_fork_stream[data_offset: data_offset+data_length], data_length)
239+
return (resource_fork_stream[rd_offset: rd_offset+rd_length], length, rd_length)
240240

241241
def appledouble_get_datafork(filepath, fileinfo):
242-
""" Returns data fork's content as bytes of appledouble file if found, otherwise empty byte string """
242+
""" Returns data fork's content as bytes and size of data fork of an appledouble file."""
243243
try:
244244
index = filepath.index("__MACOSX")
245245
except ValueError:
@@ -253,80 +253,88 @@ def appledouble_get_datafork(filepath, fileinfo):
253253

254254
try:
255255
with open(data_fork_path, "rb") as f:
256-
return f.read()
256+
data = f.read()
257+
return (data, len(data))
257258
except (FileNotFoundError, IsADirectoryError):
258259
return b''
259260

260261
def raw_rsrc_get_datafork(filepath):
261-
""" Returns the data fork's content as bytes corresponding to raw rsrc file. """
262+
""" Returns the data fork's content as bytes and size of the data fork corresponding to raw rsrc file. """
262263
try:
263264
with open(filepath[:-5]+".data", "rb") as f:
264-
return f.read()
265+
data = f.read()
266+
return (data, len(data))
265267
except (FileNotFoundError, IsADirectoryError):
266268
return b''
267269

268270
def raw_rsrc_get_resource_fork_data(filepath):
269-
""" Returns the resource fork's data section as bytes of a raw rsrc file as well as its size """
271+
""" Returns the resource fork's data section as bytes, size of resource fork (size-r) and size of data section of resource fork (size-rd) of a raw rsrc file."""
270272
with open(filepath, "rb") as f:
271273
resource_fork_stream = f.read()
272-
data_offset = int.from_bytes(resource_fork_stream[0:4])
273-
data_length = int.from_bytes(resource_fork_stream[8:12])
274+
resource_fork_len = len(resource_fork_stream)
275+
rd_offset = int.from_bytes(resource_fork_stream[0:4])
276+
rd_length = int.from_bytes(resource_fork_stream[8:12])
274277

275-
return (resource_fork_stream[data_offset: data_offset+data_length], data_length)
278+
return (resource_fork_stream[rd_offset: rd_offset+rd_length], resource_fork_len, rd_length)
276279

277280
def actual_mac_fork_get_data_fork(filepath):
278-
""" Returns the data fork's content as bytes if the actual mac fork exists """
281+
""" Returns the data fork's content as bytes and its size if the actual mac fork exists """
279282
try:
280283
with open(filepath, "rb") as f:
281-
return f.read()
284+
data = f.read()
285+
return (data, len(data))
282286
except (FileNotFoundError, IsADirectoryError):
283287
return b''
284288

285289
def actual_mac_fork_get_resource_fork_data(filepath):
286-
""" Returns the resource fork's data section as bytes of the actual mac fork as well as its size """
290+
""" Returns the resource fork's data section as bytes, size of resource fork (size-r) and size of data section of resource fork (size-rd) of the actual mac fork."""
287291
resource_fork_path = os.path.join(filepath, "..namedfork", "rsrc")
288292
with open(resource_fork_path, "rb") as f:
289293
resource_fork_stream = f.read()
290-
data_offset = int.from_bytes(resource_fork_stream[0:4])
291-
data_length = int.from_bytes(resource_fork_stream[8:12])
294+
resource_fork_len = len(resource_fork_stream)
295+
rd_offset = int.from_bytes(resource_fork_stream[0:4])
296+
rd_length = int.from_bytes(resource_fork_stream[8:12])
292297

293-
return (resource_fork_stream[data_offset: data_offset+data_length], data_length)
298+
return (resource_fork_stream[rd_offset: rd_offset+rd_length], resource_fork_len, rd_length)
294299

295-
def file_checksum(filepath, alg, size, file_info):
296-
cur_file_size = 0
300+
def file_checksum(filepath, alg, custom_checksum_size, file_info):
297301
with open(filepath, "rb") as f:
298302
if file_info[0] == FileType.NON_MAC:
299-
return (create_checksum_pairs(checksum(f, alg, size, filepath), alg, size), filesize(filepath))
303+
return (create_checksum_pairs(checksum(f, alg, custom_checksum_size, filepath), alg, custom_checksum_size), filesize(filepath), 0, 0)
300304

301305
# Processing mac files
302306
res = []
303307
resfork = b''
304308
datafork = b''
305309
file_data = f.read()
306310

311+
size = 0
312+
size_r = 0
313+
size_rd = 0
314+
307315
if file_info[0] == FileType.MAC_BINARY:
308-
(resfork, cur_file_size) = macbin_get_resfork_data(file_data)
316+
(resfork, size, size_r, size_rd) = macbin_get_resfork_data(file_data)
309317
datafork = macbin_get_datafork(file_data)
310318
elif file_info[0] in {FileType.APPLE_DOUBLE_DOT_, FileType.APPLE_DOUBLE_RSRC, FileType.APPLE_DOUBLE_MACOSX}:
311-
(resfork, cur_file_size) = appledouble_get_resfork_data(file_data)
312-
datafork = appledouble_get_datafork(filepath, file_info)
319+
(resfork, size_r, size_rd) = appledouble_get_resfork_data(file_data)
320+
(datafork, size) = appledouble_get_datafork(filepath, file_info)
313321
elif file_info[0] == FileType.RAW_RSRC:
314-
(resfork, cur_file_size) = raw_rsrc_get_resource_fork_data(filepath)
315-
datafork = raw_rsrc_get_datafork(filepath)
322+
(resfork, size_r, size_rd) = raw_rsrc_get_resource_fork_data(filepath)
323+
datafork, size = raw_rsrc_get_datafork(filepath)
316324
elif file_info[0] == FileType.ACTUAL_FORK_MAC:
317-
(resfork, cur_file_size) = actual_mac_fork_get_resource_fork_data(filepath)
318-
datafork = actual_mac_fork_get_data_fork(filepath)
325+
(resfork, size_r, size_rd) = actual_mac_fork_get_resource_fork_data(filepath)
326+
(datafork, size) = actual_mac_fork_get_data_fork(filepath)
319327

320-
hashes = checksum(resfork, alg, size, filepath)
328+
hashes = checksum(resfork, alg, custom_checksum_size, filepath)
321329
prefix = 'r'
322330
if len(resfork):
323-
res.extend(create_checksum_pairs(hashes, alg, size, prefix))
331+
res.extend(create_checksum_pairs(hashes, alg, custom_checksum_size, prefix))
324332

325-
hashes = checksum(datafork, alg, size, filepath)
333+
hashes = checksum(datafork, alg, custom_checksum_size, filepath)
326334
prefix = 'd'
327-
res.extend(create_checksum_pairs(hashes, alg, size, prefix))
335+
res.extend(create_checksum_pairs(hashes, alg, custom_checksum_size, prefix))
328336

329-
return (res, cur_file_size)
337+
return (res, size, size_r, size_rd)
330338

331339
def create_checksum_pairs(hashes, alg, size, prefix=None):
332340
res = []
@@ -571,7 +579,8 @@ def filter_files_by_timestamp(files, limit_timestamps_date):
571579
"""
572580

573581
filtered_file_map = defaultdict(str)
574-
user_date = validate_date(limit_timestamps_date)
582+
if limit_timestamp_date is not None:
583+
user_date = validate_date(limit_timestamps_date)
575584
today = date.today()
576585

577586
for filepath in files:
@@ -595,8 +604,8 @@ def create_dat_file(hash_of_dirs, path, checksum_size=0):
595604
# Game files
596605
for hash_of_dir in hash_of_dirs:
597606
file.write("game (\n")
598-
for filename, (hashes, filesize, timestamp) in hash_of_dir.items():
599-
data = f"name \"{filename}\" size {filesize} timestamp {timestamp}"
607+
for filename, (hashes, size, size_r, size_rd, timestamp) in hash_of_dir.items():
608+
data = f"name \"{filename}\" size {size} size-r {size_r} size-rd {size_rd} timestamp {timestamp}"
600609
for key, value in hashes:
601610
data += f" {key} {value}"
602611

0 commit comments

Comments
 (0)