Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Last active December 13, 2016 15:31
Show Gist options
  • Save lovasoa/409c3e4cd153185253dccd2bf69d9044 to your computer and use it in GitHub Desktop.
Save lovasoa/409c3e4cd153185253dccd2bf69d9044 to your computer and use it in GitHub Desktop.
Replace text in a binary file, padding the replacement text with spaces
#!/usr/bin/env python3
# Replace text in a binary file, padding the replacement text with spaces
import sys, locale
if len(sys.argv) != 3:
sys.stderr.write("Usage: %s string_from string_to < input.bin > output.bin.\n" % (sys.argv[0],))
sys.exit(1)
codec = locale.getpreferredencoding()
orig= bytes(sys.argv[1], codec)
txt = bytes(sys.argv[2], codec)
txt = (txt + bytes([ord(' ')] * len(orig)))[:len(orig)]
oldbin = sys.stdin.buffer.read()
newbin = oldbin.replace(orig, txt)
sys.stdout.buffer.write(newbin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment