Skip to content

Instantly share code, notes, and snippets.

@RoadrunnerWMC
Last active December 5, 2017 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RoadrunnerWMC/35e93828ddf67b50547bf7d9525cb073 to your computer and use it in GitHub Desktop.
Save RoadrunnerWMC/35e93828ddf67b50547bf7d9525cb073 to your computer and use it in GitHub Desktop.
nsmb-checksums: Updates all checksums in a New Super Mario Bros. savefile (.sav, .dsv, etc).
# nsmb-checksums.py
"""
MIT License
Copyright (c) 2017 RoadrunnerWMC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import argparse
import struct
def nsmbChecksum(data):
"""
Calculate the checksum of `data` using NSMB's savefile checksum
algorithm.
"""
checksum = sum(b'Mario2d') & 0xFFFF # = 654
for b in data:
checksum = b ^ ((2 * checksum & 0xFFFE) | (checksum >> 15) & 1)
checksum &= 0xFFFF
return checksum
def recalculateSavefileChecksums(savefile):
"""
Recalculate all checksums in the given savefile data.
"""
savefile = bytearray(savefile)
def doChecksum(checksumPos, dataLen):
"""
Recalculate the checksum at `checksumPos`, of data of length
`dataLen`.
"""
checksum = nsmbChecksum(
savefile[checksumPos + 10 : checksumPos + 10 + dataLen])
struct.pack_into('<H', savefile, checksumPos, checksum)
# Primary data / backup data
for base in [0, 0x1000]:
# Header
doChecksum(base + 0x000, 0xF4)
# Slots 1-3
doChecksum(base + 0x100, 0x248)
doChecksum(base + 0x380, 0x248)
doChecksum(base + 0x600, 0x248)
# Footer?
doChecksum(base + 0x880, 0x14)
return savefile
def main():
parser = argparse.ArgumentParser(
prog='nsmb-checksums',
formatter_class=argparse.RawDescriptionHelpFormatter,
description='Updates all checksums in a New Super Mario Bros. savefile (.sav, .dsv, etc).',
epilog=__doc__)
parser.add_argument('infile',
help='the input save file')
parser.add_argument('outfile',
help='the output save file',
nargs='?')
args = parser.parse_args()
with open(args.infile, 'rb') as f:
sav = f.read()
fixedSav = recalculateSavefileChecksums(sav)
with open(args.outfile if args.outfile else args.infile, 'wb') as f:
f.write(fixedSav)
print('Done!')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment