Welcome, guest | Sign In | My Account | Store | Cart

This script identifies every chr(xx) call in a script (being xx an integer) and replaces it with a constant byte string. For example: print chr(13) + chr(255) in the input .py file gets translated into '\n' + '\xff' on the output .py file, not breaking the input program, and maybe speeding it a little.

Python, 40 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/python
# coding:utf-8

# This script tries to identify all chr(XX) constant calls in python scripts and replace them with '\xXX' strings.

import sys
import re


def chrrepl(match):
    # See http://www.amk.ca/python/howto/regex/regex.html
    # Use the captured group to get the hex string value. 
    char_number = match.group(1)
    return repr(chr(int(char_number)))


if __name__ == '__main__':
    if len(sys.argv) != 3:
        print 'Usage: python chr_cleaner.py infile.py outfile.py'
        sys.exit(1)
    
    infile = sys.argv[1]
    outfile = sys.argv[2]
    # Open file for reading
    try:
        fin = open(infile, 'r')
        fout = open(outfile, 'w')
    except IOError:
        print 'Error when reading %s or trying to write %s' % (infile, outfile)
        sys.exit(2)
    
    intext = fin.read()
    pattern = 'chr\\((\\d+)\\)' # Group the chr() function parameter to capture it
    p = re.compile(pattern)
    outtext = p.sub(chrrepl, intext)
    fout.write(outtext)
    fout.flush()

    fin.close()
    fout.close()