Skip to content

Instantly share code, notes, and snippets.

@MegaLoler
Created February 27, 2021 00:55
Show Gist options
  • Save MegaLoler/04fe200aa5cbb80a1bb25ea1aa170263 to your computer and use it in GitHub Desktop.
Save MegaLoler/04fe200aa5cbb80a1bb25ea1aa170263 to your computer and use it in GitHub Desktop.
Extract the data and resource forks from a MacBinary II file
#!/usr/bin/env python3
# extract the data and resource forks from a mac binary 2 file
import sys
import struct
def unpack(stream, fmt):
size = struct.calcsize(fmt)
buf = stream.read(size)
return struct.unpack(fmt, buf)
def extract_from_stream(in_stream, data_stream, resource_stream):
in_stream.seek(83)
data_size = unpack(in_stream, '>i')[0]
resource_size = unpack(in_stream, '>i')[0]
print(data_size, resource_size)
in_stream.seek(128)
data_stream.write(in_stream.read(data_size))
in_stream.seek(128 + data_size + data_size % 128)
resource_stream.write(in_stream.read(resource_size))
def extract_from_path(in_path: str):
data_path = in_path + '.dat'
resource_path = in_path + '.res'
with open(in_path, 'rb') as in_stream:
with open(data_path, 'wb') as data_stream:
with open(resource_path, 'wb') as resource_stream:
extract_from_stream(in_stream, data_stream, resource_stream)
if __name__ == '__main__':
if len(sys.argv) < 2:
print(f'Usage: {sys.argv[0]} [path to file]')
else:
extract_from_path(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment