-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathstringstripper.py
More file actions
153 lines (117 loc) · 4.17 KB
/
stringstripper.py
File metadata and controls
153 lines (117 loc) · 4.17 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import re
STRING_START_MULTILINE = ["[["]
STRING_END_MULTILINE = ["]]"]
STRING_START = ['"', "'"]
STRING_END = ['"', "'"]
COMMENT = ["//", "--"]
COMMENT_START = ["/*", "--[["]
COMMENT_END = ["*/", "]]"]
ALL_SYMBOLS = STRING_START_MULTILINE + STRING_END_MULTILINE + \
STRING_START + STRING_END + COMMENT + COMMENT_START + COMMENT_END
def strip(lua):
# Start with Multi-Line Comments
lua, removed_multiline_comments = strip_multiline_comments(lua)
# Strip strings
lua, strings = strip_strings(lua)
# Strip comments
lua, removed_comments = strip_comments(lua)
# Check for failure
for s in ALL_SYMBOLS:
if s in lua:
print("Error!!! Failed to correctly strip strings.")
print("Here is a dump of the strings stripped:")
for v in strings.values():
print("\t", v)
exit()
return lua, strings, removed_multiline_comments + removed_comments
def replace(lua, strings, decrypt_func="", start="[[", end="]]"):
start = decrypt_func + start
if isinstance(lua, str):
for k, v in strings.items():
lua = lua.replace(k, start + v + end)
else:
for k, v in strings.items():
for i in range(len(lua)):
if lua[i] == k:
lua[i] = start + v + end
return lua
def strip_multiline_comments(lua):
removed = []
for i in range(len(COMMENT_START)):
r = _build_regex(COMMENT_START[i], COMMENT_END[i])
while True:
match = r.search(lua)
if match is None:
break
removed.append(match.group(0))
lua = lua[:match.start()] + lua[match.end():]
print(match)
return lua, removed
def strip_comments(lua):
removed = []
for i in range(len(COMMENT)):
r = _build_regex(COMMENT[i])
while True:
match = r.search(lua)
if match is None:
break
removed.append(match.group())
lua = lua[:match.start()] + lua[match.end():]
return lua, removed
def strip_strings(lua):
lua, removed = _strip_multiline_strings(lua)
lua, _removed = _strip_regular_strings(lua)
removed.update(_removed)
for k in removed.keys():
removed[k] = removed[k].encode("utf-8").decode("unicode_escape")
return lua, removed
def _strip_regular_strings(lua):
removed = {}
patterns = []
for i in range(len(STRING_START)):
start, end = STRING_START[i], STRING_END[i]
patterns.append(_build_regex(start, end, False))
while True:
found = None
first = len(lua) + 1
# Find the first match
for r in patterns:
match = r.search(lua)
if match is not None:
if found is None or match.start() < first:
found = match
first = match.start()
if found is not None:
placeholder = _build_string_placeholder()
removed[placeholder] = found.group()[len(start):-len(end)]
lua = lua[:found.start()] + " " + placeholder + " " + lua[found.end():]
else:
break
return lua, removed
def _strip_multiline_strings(lua):
removed = {}
for i in range(len(STRING_START_MULTILINE)):
start, end = STRING_START_MULTILINE[i], STRING_END_MULTILINE[i]
r = _build_regex(start, end)
while True:
match = r.search(lua)
if match is None:
break
placeholder = _build_string_placeholder()
removed[placeholder] = match.group()[len(start):-len(end)]
lua = lua[:match.start()] + placeholder + lua[match.end():]
return lua, removed
string_index = -1
def _build_string_placeholder():
global string_index
string_index += 1
return "__STRING_{0}__".format(string_index)
def _build_regex(start, end=None, multiline=True):
if end is None:
# Return a simple single line regex
return re.compile(re.escape(start) + r".*?$", re.MULTILINE)
if multiline:
rs = re.escape(start) + r"(.|\n|\r)*?" + re.escape(end)
else:
rs = start + r'(?:[^' + end + r'\\]|\\.)*' + end
return re.compile(rs)