Created
March 18, 2021 04:55
-
-
Save depp/5d351a871078213b6a9188e27bbf3b4d to your computer and use it in GitHub Desktop.
Convert resource fork files to MacBinary files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""rsrc2macbinary.py - convert resource fork to MacBinary file""" | |
import datetime | |
import os | |
import struct | |
import sys | |
import zlib | |
def die(*msg): | |
print('Error:', *msg, file=sys.stderr) | |
raise SystemExit(1) | |
EPOCH = datetime.datetime(1904, 1, 1) | |
def main(argv): | |
if len(argv) != 2: | |
die('usage: python rsrc2macbinary.py <input.rsrc> <output.bin>') | |
inpath, outpath = argv | |
st = os.stat(inpath) | |
mtime = datetime.datetime.utcfromtimestamp(st.st_mtime) | |
mtime_num = int((mtime - EPOCH).total_seconds()) | |
with open(inpath, 'rb') as fp: | |
data = fp.read() | |
name = os.path.splitext(os.path.basename(outpath))[0] | |
header = struct.pack( | |
'>B64p4s4sBBhhhBBIIIIhB14sIhBB', | |
0, # old version number | |
name.encode('ASCII'), # name | |
b'rsrc', # file type | |
b'RSED', # file creator | |
0, # finder flags | |
0, # zero fill | |
0, 0, # position in finder window | |
0, # window or folder ID | |
0, # protected | |
0, # zero fill | |
0, # data fork length | |
len(data), # rsrc fork length | |
mtime_num, # creation date | |
mtime_num, # last modified | |
0, # get info comment length | |
0, # finder flags | |
b'', # padding | |
0, # unpacked length | |
0, # secondary header length | |
129, # version of MacBinary used to create | |
129, # version of MacBinary needed to extract | |
) | |
with open(outpath, 'wb') as fp: | |
fp.write(header) | |
fp.write(struct.pack('>I', zlib.crc32(header))) | |
fp.write(data) | |
fp.write(b'\x00' * ((-len(data)) & 127)) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment