-
Notifications
You must be signed in to change notification settings - Fork 0
/
shiftops.py
54 lines (43 loc) · 1.33 KB
/
shiftops.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import dis
from pprint import pprint
revmap = {dis.opmap[x]: x for x in dis.opmap}
def log(*args):
#print args
pass
def shift_ops(codestring, start, count, limit=True):
i = 0
r = codestring
while i < len(codestring) and (limit or i < start):
op = ord(codestring[i])
opi = i
log(opi, revmap.get(op, '<%d>' % op))
i += 1
if op in dis.hasjabs:
replrange = codestring[i:i+2]
oparg = ord(replrange[0]) + ord(replrange[1]) * 256
if oparg >= start:
newtarget = oparg + count
newtargethex = chr(newtarget % 256) + chr(newtarget / 256)
codestring = codestring[:i] + newtargethex + codestring[i+2:]
i += 2
elif op > dis.HAVE_ARGUMENT:
i += 2
if limit is True:
munge = 0
r = codestring[:start+1-munge] + '\x09' * (count + munge) + codestring[start+1:]
return r
def replace(c, i, s):
return c[:i] + s + c[i + len(s):]
def strip_nops(codestring):
i = 0
while i < len(codestring):
op = ord(codestring[i])
i += 1
if op is 9:
i -= 1
codestring = codestring[:i] + codestring[i+1:]
elif op in dis.hasjabs:
i += 2
elif op > dis.HAVE_ARGUMENT:
i += 2
return codestring