-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_info.py
More file actions
290 lines (244 loc) · 10.6 KB
/
api_info.py
File metadata and controls
290 lines (244 loc) · 10.6 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import re
import xml.etree.ElementTree as ET
from collections import namedtuple
def is_anonymous_or_lambda(c):
# three condition: anonymous class, lambda expression at >= 30, lambda expression at < 30
return re.search(r'\$\d+$', c) or re.search(r'\$\$ExternalSyntheticLambda\d+$', c) or re.search(r'-\$\$Lambda\$', c) or c.endswith('$$')
def is_anonymous_field(f, c):
return '$' in f
def is_anonymous_method(m, c):
return '$' in m
FieldSub = namedtuple('FieldSub', ['class_name', 'name'])
MethodSub = namedtuple('MethodSub', ['class_name', 'name', 'parameter_types'])
class ApiConverter:
@staticmethod
def jni_to_java_single(jni_type):
if jni_type.startswith('Z'):
return 'boolean', jni_type[1:]
elif jni_type.startswith('B'):
return 'byte', jni_type[1:]
elif jni_type.startswith('C'):
return 'char', jni_type[1:]
elif jni_type.startswith('S'):
return 'short', jni_type[1:]
elif jni_type.startswith('I'):
return 'int', jni_type[1:]
elif jni_type.startswith('J'):
return 'long', jni_type[1:]
elif jni_type.startswith('F'):
return 'float', jni_type[1:]
elif jni_type.startswith('D'):
return 'double', jni_type[1:]
elif jni_type.startswith('V'):
return 'void', jni_type[1:]
elif jni_type.startswith('L'):
end = jni_type.index(';')
type_str = jni_type[1:end].replace('/', '.')
return type_str, jni_type[end+1:]
elif jni_type.startswith('['):
remain = jni_type[1:]
t, jni_type = ApiConverter.jni_to_java_single(remain)
return f'{t}[]', jni_type
@staticmethod
def jni_to_java(type_str):
types = []
while type_str:
t, type_str = ApiConverter.jni_to_java_single(type_str)
types.append(t)
return tuple(types)
# example: ApiConverter("android-33/hiddenapi-flags.csv")
def __init__(self, file_path):
self.api_level = 0
self.api_file = ''
self.classes = []
self.fields = []
self.methods = []
def report(self):
print(f'{self.api_file}({self.api_level}): #class={len(self.classes)}, #field={len(self.fields)}, #method={len(self.methods)}')
def to_sublist(self, store_path):
def inclusion_exclusion(c): # generated class already dropped at loading
return True
# return is_android_package(c) and not is_external(c)
class_lines = []
for clazz in self.classes:
if inclusion_exclusion(clazz):
class_line = clazz + '\n'
class_lines.append(class_line)
class_lines.sort()
class_filename = f'/{self.api_file}-class.txt'
with open(store_path+class_filename, 'w') as w:
for class_line in class_lines:
w.write(class_line)
field_lines = []
for field in self.fields:
if inclusion_exclusion(field.class_name):
field_line = f'{field.class_name}->{field.name}\n'
field_lines.append(field_line)
field_lines.sort()
field_filename = f'/{self.api_file}-field.txt'
with open(store_path+field_filename, 'w') as w:
for field_line in field_lines:
w.write(field_line)
method_lines = []
for method in self.methods:
if inclusion_exclusion(method.class_name):
parameters = ','.join(method.parameter_types)
method_line = f'{method.class_name}->{method.name}({parameters})\n'
method_lines.append(method_line)
method_lines.sort()
method_filename = f'/{self.api_file}-method.txt'
with open(store_path+method_filename, 'w') as w:
for method_line in method_lines:
w.write(method_line)
def get_apis(self):
return self.classes, self.fields, self.methods
class Hiddenapi_ApiConverter(ApiConverter):
report_category = 'Hidden-APIs'
field_pattern = r'^(.+?)->(.+?):(.+?)$'
method_pattern = r'^(.+?)->(.+?)\((.*?)\)(.+?)$'
def __init__(self, file_path):
super().__init__(file_path)
assert(file_path.endswith('hiddenapi-flags.csv'))
self.api_level = int(re.search(r'android-(\d+)', file_path)[1])
self.api_file = 'CSV'
classes = set()
with open(file_path) as r:
for line in r:
api = line.split(',')[0]
fm = re.match(self.field_pattern, api)
mm = re.match(self.method_pattern, api)
if fm:
cn, name, t = fm.groups()
class_name = self.jni_to_java(cn)[0]
if is_anonymous_or_lambda(class_name):
continue
if is_anonymous_field(name, class_name):
continue
assert name.isidentifier(), api
# type = self.jni_to_java(t)[0]
field = FieldSub(class_name, name)
# print(field)
self.fields.append(field)
classes.add(class_name)
elif mm:
cn, name, p, t = mm.groups()
class_name = self.jni_to_java(cn)[0]
if is_anonymous_or_lambda(class_name):
continue
if is_anonymous_method(name, class_name) or name=='<clinit>':
continue
assert name=='<init>' or name.isidentifier(), api
parameter_types = self.jni_to_java(p)
# return_type = self.jni_to_java(t)[0]
method = MethodSub(class_name, name, parameter_types)
# print(method)
self.methods.append(method)
classes.add(class_name)
else:
print('error: ' + api)
exit()
self.classes.extend(classes)
class Apiversions_ApiConverter(ApiConverter):
method_pattern = r'^(.+?)\((.*?)\)(.+?)$'
def __init__(self, file_path):
super().__init__(file_path)
assert(file_path.endswith('api-versions.xml'))
self.api_level = int(re.search(r'android-(\d+)', file_path)[1])
self.api_file = 'XML'
classes = set()
tree = ET.parse(file_path)
for clazz in tree.findall('./class'):
if clazz.get('removed') is not None:
continue
class_name = clazz.get('name').replace('/', '.')
classes.add(class_name)
for field in clazz.findall('./field'):
if field.get('removed') is not None:
continue
name = field.get('name')
field = FieldSub(class_name, name)
# print(field)
self.fields.append(field)
for method in clazz.findall('./method'):
if method.get('removed') is not None:
continue
mm = re.match(self.method_pattern, method.get('name'))
name, p, t = mm.groups()
parameter_types = self.jni_to_java(p)
# return_type = self.jni_to_java(t)[0]
method = MethodSub(class_name, name, parameter_types)
# print(method)
self.methods.append(method)
self.classes.extend(classes)
class Androidjar_ApiConverter(ApiConverter):
field_pattern = r'^<(.*?): (.*?) ([^()]*?)>$'
method_pattern = r'^<(.*?): (.*?) (.*?)\((.*?)\)>$'
def __init__(self, file_path):
super().__init__(file_path)
assert(file_path.endswith('android.jar.txt'))
self.api_level = int(re.search(r'android-(\d+)', file_path)[1])
self.api_file = 'JAR'
classes = set()
with open(file_path) as r:
for api in r:
fm = re.match(self.field_pattern, api)
mm = re.match(self.method_pattern, api)
if fm:
class_name = fm.group(1)
name = fm.group(3)
if name in {'$VALUES', 'this$0'}:
continue
field = FieldSub(class_name, name)
self.fields.append(field)
elif mm:
class_name = mm.group(1)
name = mm.group(3)
if name in {'<clinit>'}:
continue
parameters = mm.group(4)
if parameters == '':
parameter_types = tuple()
else:
parameter_types = tuple(p for p in parameters.split(','))
method = MethodSub(class_name, name, parameter_types)
# print('method '+ class_name + ' ' + name + ' ' + parameter_types)
self.methods.append(method)
else:
class_name = api.strip()
if re.search(r'\$\d+$', class_name):
continue
classes.add(class_name)
self.classes.extend(classes)
class Currenttxt_ApiConverter(ApiConverter):
field_pattern = r'^<(.*?): (.*?) ([^()]*?)>$'
method_pattern = r'^<(.*?): (.*?) (.*?)\((.*?)\)>$'
def __init__(self, file_path):
super().__init__(file_path)
assert(file_path.endswith('current.txt.txt'))
self.api_level = int(re.search(r'android-(\d+)', file_path)[1])
self.api_file = 'TXT'
with open(file_path, mode='r', encoding='utf-8-sig') as r:
classes = set()
for api in map(str.strip, r):
fm = re.match(self.field_pattern, api)
mm = re.match(self.method_pattern, api)
if fm:
class_name = fm.group(1)
name = fm.group(3)
field = FieldSub(class_name, name)
self.fields.append(field)
classes.add(class_name)
elif mm:
class_name = mm.group(1)
name = mm.group(3)
parameters = mm.group(4)
if parameters == '':
parameter_types = tuple()
else:
parameter_types = tuple(p for p in parameters.split(','))
method = MethodSub(class_name, name, parameter_types)
self.methods.append(method)
classes.add(class_name)
else:
classes.add(api)
self.classes.extend(classes)