Blob Blame History Raw
import sys

OLD_PATH = b"/usr/lib/sox\x00"
NEW_PATH = b"/var/tmp/sox\x00"
OUT_PATH =  "/var/tmp/sox\n"
OLD64_PATH = b"/usr/lib64/sox\x00"
NEW64_PATH = b"/var/tmp/l/sox\x00"
OUT64_PATH =  "/var/tmp/l/sox\n"

assert len(OLD_PATH) == len(NEW_PATH)
assert len(OLD64_PATH) == len(NEW64_PATH)

argv = sys.argv
wout = sys.stdout.write
werr = sys.stderr.write
bye = sys.exit

def p(m, *args):
    werr(m % args)

def usage():
    p("Usage: %s <path to libsox>\n", argv[0])

def error(et):
    def ef():
        p("%s: %r cannot be binary patched [%s]\n", argv[0], argv[1], et)
    return ef

def check(c, p):
    if not c:
        p()
        bye(1)

check(len(argv) == 2, usage)

s = b""
o = ""
with open(argv[1], 'rb') as f:
    s = f.read()
if s.count(OLD_PATH) == 1:
    check(s.count(OLD64_PATH) == 0, error("OLD64_PATH"))
    s = s.replace(OLD_PATH, NEW_PATH)
    o = OUT_PATH
elif s.count(OLD64_PATH) == 1:
    check(s.count(OLD_PATH) == 0, error("OLD_PATH"))
    s = s.replace(OLD64_PATH, NEW64_PATH)
    o = OUT64_PATH
else:
    check(False, error("OLD_PATH/OLD64_PATH"))
with open(argv[1], 'wb') as f:
    f.write(s)
wout(o)