Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Utility to strip metadata from NSF2 files.
#!/usr/bin/env python3
import sys
assert sys.version_info[0] >= 3, "Python 3 required."
#
# nsf2_strip.py
# Brad Smith, 2018-08-25
#
# This strips metadata from NSF2 files.
#
import struct
in_nsf2 = "in.nsf"
out_nsf = "out.nsf"
show_cut = True
def nsf2_strip(nsf2):
if len(nsf2) < 0x80:
raise Exception("NSF2 too short for header?")
nsf = bytearray(nsf2)
version = nsf[5]
nsf2_bits = nsf[0x7E]
if version >= 2 and (nsf2_bits & 0x80) != 0:
raise Exception("NSF2 metadata is flagged as essential.")
suffix = nsf[0x7D] | (nsf[0x7E]<<8) | (nsf[0x7F]<<16)
nsf[0x7D] = 0
nsf[0x7E] = 0
nsf[0x7F] = 0
stripped = 0
if suffix != 0:
cut = suffix + 0x80
if cut > len(nsf):
raise Exception("NSF2 metadata pointer is past end of file?")
stripped = len(nsf) - cut
nsf = nsf[0:cut]
if show_cut:
print("%d bytes stripped." % stripped)
return nsf
print("Reading " + in_nsf2 + "...")
nsf2 = open(in_nsf2,"rb").read()
nsf = nsf2_strip(nsf2)
print("Saving " + out_nsf + "...")
open(out_nsf,"wb").write(nsf)
print ("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.