Last active
December 13, 2016 15:31
-
-
Save lovasoa/409c3e4cd153185253dccd2bf69d9044 to your computer and use it in GitHub Desktop.
Replace text in a binary file, padding the replacement text with spaces
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
#!/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