-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathScriptMaker.py
212 lines (189 loc) · 6.79 KB
/
ScriptMaker.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
class ScriptMaker:
tab = "\t"*2
def __init__(self, cls, method):
self.cls = cls
self.method = method
def arg_make(self, args):
arg_code = ""
if len(args) != 0:
arg_code += self.tab+"var p=[];\n"
i = 0
for arg in args:
print(arg)
arg_code += self.arg_types(arg)(i, arg)
i+=1
arg_code += self.tab+self.send_arg()+"\n"
return arg_code
def ret_make(self, ret):
return self.tab+self.ret_types(ret)(ret)+"\n"
def arg_types(self, arg_type):
if "java.lang.String" == arg_type:
return self.arg_string
elif arg_type in ["java.util.List", "java.util.ArrayList"]:
return self.arg_list
elif arg_type in ["java.util.Map", "java.util.HashMap", "android.os.Bundle"]:
return self.arg_map
elif arg_type in ["[B"]:
return self.arg_bytearray
else:
return self.arg_force_string
def ret_types(self, ret_type):
if "java.lang.String" == ret_type:
return self.ret_string
elif "void" == ret_type:
return self.ret_void
elif ret_type in ["java.util.List", "java.util.ArrayList"]:
return self.ret_list
elif ret_type in ["java.util.Map", "java.util.HashMap", "android.os.Bundle"]:
return self.ret_map
elif ret_type in ["[B"]:
return self.ret_bytearray
else:
return self.ret_force_string
def arg_string(self, index, t):
code = self.tab+"""p.push({{"{0}": arguments[{1}]}});\n""".format(t, index)
return code
def arg_force_string(self, index, t):
code = self.tab+"""p.push({{"{0}": String(arguments[{1}])}});\n""".format(t, index)
return code
def arg_list(self, index, t):
code = """if(arguments[{0}] != null){{
var p_{0} = [];
for(var i = 0;i<arguments[{0}].size();i++){{
p_{0}.push(String(arguments[{0}].get(i)));
}}
p.push({{"{1}":p_{0}}});
}}else{{
p.push({{"{1}":"null"}});
}}""".format(index, t)
code = "\n".join(self.tab+x for x in code.split("\n"))+"\n"
return code
def ret_list(self, t):
code = """if(retval != null){{
var r = [];
for(var i = 0;i<retval.size();i++){{
r.push(String(retval.get(i)));
}}
send_ret("{1}","{2}","{0}",r);
}}else{{
send_ret("{1}","{2}","{0}","null");
}}""".format(t, self.cls, self.method)
code = "\n".join(self.tab+x for x in code.split("\n"))+"\n"
return code
def arg_map(self, index, t):
code = """if(arguments[{0}] != null){{
var json = {{}};
var iter = arguments[{0}].keySet().iterator();
while(iter.hasNext()){{
var key = iter.next();
json[key] = String(arguments[{0}].get(key));
}}
p.push({{"{1}":JSON.stringify(json)}});
}}else{{
p.push({{"{1}":"null"}});
}}""".format(index, t)
code = "\n".join(self.tab+x for x in code.split("\n"))+"\n"
return code
def ret_map(self, t):
code = """if(retval != null){{
var json = {{}};
var iter = retval.keySet().iterator();
while(iter.hasNext()){{
var key = iter.next();
json[key] = String(retval.get(key));
}}
send_ret("{1}","{2}","{0}",json);
}}else{{
send_ret("{1}","{2}","{0}","null");
}}""".format(t, self.cls, self.method)
code = "\n".join(self.tab+x for x in code.split("\n"))+"\n"
return code
def arg_bytearray(self, index, t):
code = """if(arguments[{0}] != null){{
var p_{0} = Memory.alloc(arguments[{0}].length);
var arr_{0} = [];
for(var i = 0;i<arguments[{0}].length;i++){{
arr_{0}.push(arguments[{0}][i]);
}}
p_{0}.writeByteArray(arr_{0});
p.push({{"{1}":"\\n"+hexdump(p_{0},{{length: arguments[{0}].length}})}});
}}else{{
p.push({{"{1}":"null"}});
}}""".format(index, t)
code = "\n".join(self.tab+x for x in code.split("\n"))+"\n"
return code
def ret_bytearray(self, t):
code = """if(retval != null){{
var r = Memory.alloc(retval.length);
var arr = [];
for(var i = 0;i<retval.length;i++){{
arr.push(retval[i]);
}}
r.writeByteArray(arr);
send_ret("{1}","{2}","{0}","\\n"+hexdump(r,{{length: retval.length}}));
}}else{{
send_ret("{1}","{2}","{0}","null");
}}""".format(t, self.cls, self.method)
code = "\n".join(self.tab+x for x in code.split("\n"))+"\n"
return code
def send_arg(self):
code = """send_arg("{0}", "{1}", p);""".format(self.cls, self.method)
return code
def ret_string(self, t):
code = """send_ret("{1}","{2}","{0}",retval);""".format(t, self.cls, self.method)
return code
def ret_force_string(self, t):
code = """send_ret("{1}","{2}","{0}",String(retval));""".format(t, self.cls, self.method)
return code
def ret_void(self, t):
return ""
class ScriptMaker_IOS:
tab = "\t"*2
def __init__(self, cls, method):
self.cls = cls
self.method = method
def arg_make(self, args):
arg_code = ""
if len(args) != 0:
if len(args) > 2:
arg_code += self.tab+"var p=[];\n"
i = 0
for arg in args:
if i > 1:
arg_code += self.arg_types(arg)(i, arg)
i+=1
if len(args) > 2:
arg_code += self.tab+self.send_arg()
return arg_code
def ret_make(self, ret):
return self.tab+self.ret_types(ret)(ret)
def arg_types(self, arg_type):
if arg_type in ["pointer"]:
return self.arg_pointer
else:
return self.arg_force_object
def ret_types(self, ret_type):
if ret_type in ["pointer"]:
return self.ret_pointer
elif ret_type == "void":
return self.ret_void
else:
return self.ret_force_object
def arg_pointer(self, index, t):
code = """p.push({{"{0}": (new ObjC.Object(args[{1}])).toString()}});""".format(t, index)
code = "\n".join(self.tab+x for x in code.split("\n"))+"\n"
return code
def arg_force_object(self, index, t):
code = self.tab+"""p.push({{"{0}": args[{1}]}});\n""".format(t, index)
return code
def send_arg(self):
code = """send_arg("{0}", "{1}", p, this.context);""".format(self.cls, self.method)
return code
def ret_pointer(self, t):
code = """send_ret("{1}","{2}","{0}", (new ObjC.Object(retval)).toString(), this.context);""".format(t, self.cls, self.method)
return code
def ret_force_object(self, t):
code = self.tab+"""send_ret("{1}","{2}","{0}", retval, this.context);\n""".format(t, self.cls, self.method)
return code
def ret_void(self, t):
return ""