summaryrefslogtreecommitdiff
path: root/python/typeprinter.py
blob: 354084dd5500dbe3719597318ec76612ff321a69 (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Copyright (c) 2015-2022 Vector 35 Inc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import abc
import ctypes
import dataclasses
from json import dumps
from typing import List, Tuple, Optional

import sys
import traceback

# Binary Ninja Components
import binaryninja
import binaryninja._binaryninjacore as core

from .settings import Settings
from . import platform as _platform
from . import types
from . import function as _function
from . import binaryview
from .log import log_error
from .enums import TokenEscapingType


def to_bytes(field):
	if type(field) == bytes:
		return field
	if type(field) == str:
		return field.encode()
	return str(field).encode()


class _TypePrinterMetaclass(type):
	def __iter__(self):
		binaryninja._init_plugins()
		count = ctypes.c_ulonglong()
		types = core.BNGetTypePrinterList(count)
		try:
			for i in range(0, count.value):
				yield CoreTypePrinter(types[i])
		finally:
			core.BNFreeTypePrinterList(types)

	def __getitem__(self, value):
		binaryninja._init_plugins()
		handle = core.BNGetTypePrinterByName(str(value))
		if handle is None:
			raise KeyError(f"'{value}' is not a valid TypePrinter")
		return CoreTypePrinter(handle)

	@property
	def default(self):
		name = binaryninja.Settings().get_string("analysis.types.printerName")
		return CoreTypePrinter[name]


class TypePrinter(metaclass=_TypePrinterMetaclass):
	name = None
	_registered_printers = []
	_cached_tokens = None
	_cached_string = None
	_cached_error = None

	def __init__(self, handle=None):
		if handle is not None:
			self.handle = core.handle_of_type(handle, core.BNTypePrinter)
			self.__dict__["name"] = core.BNGetTypePrinterName(handle)

	def register(self):
		assert self.__class__.name is not None

		self._cb = core.BNTypePrinterCallbacks()
		self._cb.context = 0
		self._cb.getTypeTokens = self._cb.getTypeTokens.__class__(self._get_type_tokens)
		self._cb.getTypeTokensBeforeName = self._cb.getTypeTokensBeforeName.__class__(self._get_type_tokens_before_name)
		self._cb.getTypeTokensAfterName = self._cb.getTypeTokensAfterName.__class__(self._get_type_tokens_after_name)
		self._cb.getTypeString = self._cb.getTypeString.__class__(self._get_type_string)
		self._cb.getTypeStringBeforeName = self._cb.getTypeStringBeforeName.__class__(self._get_type_string_before_name)
		self._cb.getTypeStringAfterName = self._cb.getTypeStringAfterName.__class__(self._get_type_string_after_name)
		self._cb.getTypeLines = self._cb.getTypeLines.__class__(self._get_type_lines)
		self._cb.freeTokens = self._cb.freeTokens.__class__(self._free_tokens)
		self._cb.freeString = self._cb.freeString.__class__(self._free_string)
		self._cb.freeLines = self._cb.freeLines.__class__(self._free_lines)
		self.handle = core.BNRegisterTypePrinter(self.__class__.name, self._cb)
		self.__class__._registered_printers.append(self)

	def __str__(self):
		return f'<TypePrinter: {self.name}>'

	def __repr__(self):
		return f'<TypePrinter: {self.name}>'

	def _get_type_tokens(self, ctxt, type, platform, name, base_confidence, escaping, result, result_count):
		try:
			platform_py = None
			if platform:
				platform_py = _platform.Platform(handle=core.BNNewPlatformReference(platform))
			result_py = self.get_type_tokens(
				types.Type(handle=core.BNNewTypeReference(type)), platform_py,
				types.QualifiedName._from_core_struct(name.contents), base_confidence, escaping)

			TypePrinter._cached_tokens = _function.InstructionTextToken._get_core_struct(result_py)
			result[0] = TypePrinter._cached_tokens
			result_count[0] = len(result_py)

			return True
		except:
			log_error(traceback.format_exc())
			return False

	def _get_type_tokens_before_name(self, ctxt, type, platform, base_confidence, parent_type, escaping, result, result_count):
		try:
			platform_py = None
			if platform:
				platform_py = _platform.Platform(handle=core.BNNewPlatformReference(platform))
			parent_type_py = None
			if parent_type:
				parent_type_py = types.Type(handle=core.BNNewTypeReference(parent_type))
			result_py = self.get_type_tokens_before_name(
				types.Type(handle=core.BNNewTypeReference(type)), platform_py,
				base_confidence, parent_type_py, escaping)

			TypePrinter._cached_tokens = _function.InstructionTextToken._get_core_struct(result_py)
			result[0] = TypePrinter._cached_tokens
			result_count[0] = len(result_py)

			return True
		except:
			log_error(traceback.format_exc())
			return False

	def _get_type_tokens_after_name(self, ctxt, type, platform, base_confidence, parent_type, escaping, result, result_count):
		try:
			platform_py = None
			if platform:
				platform_py = _platform.Platform(handle=core.BNNewPlatformReference(platform))
			parent_type_py = None
			if parent_type:
				parent_type_py = types.Type(handle=core.BNNewTypeReference(parent_type))
			result_py = self.get_type_tokens_after_name(
				types.Type(handle=core.BNNewTypeReference(type)), platform_py,
				base_confidence, parent_type_py, escaping)

			TypePrinter._cached_tokens = _function.InstructionTextToken._get_core_struct(result_py)
			result[0] = TypePrinter._cached_tokens
			result_count[0] = len(result_py)

			return True
		except:
			log_error(traceback.format_exc())
			return False

	def _get_type_string(self, ctxt, type, platform, name, escaping, result):
		try:
			platform_py = None
			if platform:
				platform_py = _platform.Platform(handle=core.BNNewPlatformReference(platform))
			result_py = self.get_type_string(
				types.Type(handle=core.BNNewTypeReference(type)), platform_py,
				types.QualifiedName._from_core_struct(name.contents), escaping)

			TypePrinter._cached_string = core.cstr(result_py)
			result[0] = TypePrinter._cached_string
			return True
		except:
			log_error(traceback.format_exc())
			return False

	def _get_type_string_before_name(self, ctxt, type, platform, escaping, result):
		try:
			platform_py = None
			if platform:
				platform_py = _platform.Platform(handle=core.BNNewPlatformReference(platform))
			result_py = self.get_type_string_before_name(
				types.Type(handle=core.BNNewTypeReference(type)), platform_py,
				escaping)

			TypePrinter._cached_string = core.cstr(result_py)
			result[0] = TypePrinter._cached_string
			return True
		except:
			log_error(traceback.format_exc())
			return False

	def _get_type_string_after_name(self, ctxt, type, platform, escaping, result):
		try:
			platform_py = None
			if platform:
				platform_py = _platform.Platform(handle=core.BNNewPlatformReference(platform))
			result_py = self.get_type_string_after_name(
				types.Type(handle=core.BNNewTypeReference(type)), platform_py,
				escaping)

			TypePrinter._cached_string = core.cstr(result_py)
			result[0] = TypePrinter._cached_string
			return True
		except:
			log_error(traceback.format_exc())
			return False

	def _get_type_lines(self, ctxt, type, data, name, line_width, collapsed, escaping, result, result_count):
		try:
			result_py = self.get_type_lines(
				types.Type(handle=core.BNNewTypeReference(type)),
				binaryview.BinaryView(handle=core.BNNewViewReference(data)),
				types.QualifiedName._from_core_struct(name.contents),
				line_width, collapsed, escaping)

			TypePrinter._cached_lines = (core.BNTypeDefinitionLine * len(result_py))()
			for (i, line) in enumerate(result_py):
				TypePrinter._cached_lines[i] = line._to_core_struct()
			result[0] = TypePrinter._cached_lines
			result_count[0] = len(result_py)

			return True
		except:
			log_error(traceback.format_exc())
			return False

	def _free_tokens(self, ctxt, tokens, count):
		try:
			TypePrinter._cached_tokens = None
			return True
		except:
			log_error(traceback.format_exc())
			return False

	def _free_string(self, ctxt, string):
		try:
			TypePrinter._cached_string = None
			return True
		except:
			log_error(traceback.format_exc())
			return False

	def _free_lines(self, ctxt, lines, count):
		try:
			for line in TypePrinter._cached_lines:
				core.BNFreeType(line.type)
				core.BNFreeType(line.rootType)
			TypePrinter._cached_lines = None
			return True
		except:
			log_error(traceback.format_exc())
			return False

	def get_type_tokens(self, type: types.Type, platform: Optional[_platform.Platform] = None, name: types.QualifiedNameType = "", base_confidence: int = core.max_confidence, escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> List[_function.InstructionTextToken]:
		raise NotImplementedError()

	def get_type_tokens_before_name(self, type: types.Type, platform: Optional[_platform.Platform] = None, base_confidence: int = core.max_confidence, parent_type: Optional[types.Type] = None, escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> List[_function.InstructionTextToken]:
		raise NotImplementedError()

	def get_type_tokens_after_name(self, type: types.Type, platform: Optional[_platform.Platform] = None, base_confidence: int = core.max_confidence, parent_type: Optional[types.Type] = None, escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> List[_function.InstructionTextToken]:
		raise NotImplementedError()

	def get_type_string(self, type: types.Type, platform: Optional[_platform.Platform] = None, name: types.QualifiedNameType = "", escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> str:
		raise NotImplementedError()

	def get_type_string_before_name(self, type: types.Type, platform: Optional[_platform.Platform] = None, escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> str:
		raise NotImplementedError()

	def get_type_string_after_name(self, type: types.Type, platform: Optional[_platform.Platform] = None, escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> str:
		raise NotImplementedError()

	def get_type_lines(self, type: types.Type, data: binaryview.BinaryView, name: types.QualifiedNameType, line_width = 80, collapsed = False, escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> List[types.TypeDefinitionLine]:
		raise NotImplementedError()


class CoreTypePrinter(TypePrinter):

	def get_type_tokens(self, type: types.Type, platform: Optional[_platform.Platform] = None,
						name: types.QualifiedNameType = "", base_confidence: int = core.max_confidence,
						escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> List[
		_function.InstructionTextToken]:
		if not isinstance(name, types.QualifiedName):
			name = types.QualifiedName(name)
		count = ctypes.c_ulonglong()
		name_cpp = name._to_core_struct()
		result_cpp = ctypes.POINTER(core.BNInstructionTextToken)()
		if not core.BNGetTypePrinterTypeTokens(self.handle, type.handle, None if platform is None else platform.handle, name_cpp, base_confidence, ctypes.c_int(escaping), result_cpp, count):
			raise RuntimeError("BNGetTypePrinterTypeTokens returned False")

		result = _function.InstructionTextToken._from_core_struct(result_cpp, count.value)
		core.BNFreeInstructionText(result_cpp.contents, count.value)
		return result

	def get_type_tokens_before_name(self, type: types.Type, platform: Optional[_platform.Platform] = None,
									base_confidence: int = core.max_confidence, parent_type: Optional[types.Type] = None,
									escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> List[
		_function.InstructionTextToken]:
		count = ctypes.c_ulonglong()
		result_cpp = ctypes.POINTER(core.BNInstructionTextToken)()
		parent_type_cpp = None
		if parent_type is not None:
			parent_type_cpp = parent_type.handle
		if not core.BNGetTypePrinterTypeTokensBeforeName(self.handle, type.handle, None if platform is None else platform.handle, base_confidence, parent_type_cpp, ctypes.c_int(escaping), result_cpp, count):
			raise RuntimeError("BNGetTypePrinterTypeTokensBeforeName returned False")

		result = _function.InstructionTextToken._from_core_struct(result_cpp, count.value)
		core.BNFreeInstructionText(result_cpp.contents, count.value)
		return result

	def get_type_tokens_after_name(self, type: types.Type, platform: Optional[_platform.Platform] = None,
								   base_confidence: int = core.max_confidence, parent_type: Optional[types.Type] = None,
								   escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> List[
		_function.InstructionTextToken]:
		count = ctypes.c_ulonglong()
		result_cpp = ctypes.POINTER(core.BNInstructionTextToken)()
		parent_type_cpp = None
		if parent_type is not None:
			parent_type_cpp = parent_type.handle
		if not core.BNGetTypePrinterTypeTokensAfterName(self.handle, type.handle, None if platform is None else platform.handle, base_confidence, parent_type_cpp, ctypes.c_int(escaping), result_cpp, count):
			raise RuntimeError("BNGetTypePrinterTypeTokensAfterName returned False")

		result = _function.InstructionTextToken._from_core_struct(result_cpp, count.value)
		core.BNFreeInstructionText(result_cpp.contents, count.value)
		return result

	def get_type_string(self, type: types.Type, platform: Optional[_platform.Platform] = None,
						name: types.QualifiedNameType = "",
						escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> str:
		if not isinstance(name, types.QualifiedName):
			name = types.QualifiedName(name)
		result_cpp = ctypes.c_char_p()
		if not core.BNGetTypePrinterTypeString(self.handle, type.handle, None if platform is None else platform.handle, name._to_core_struct(), ctypes.c_int(escaping), result_cpp):
			raise RuntimeError("BNGetTypePrinterTypeString returned False")

		result = core.pyNativeStr(result_cpp.value)
		core.free_string(result_cpp)
		return result

	def get_type_string_before_name(self, type: types.Type, platform: Optional[_platform.Platform] = None,
									escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> str:
		result_cpp = ctypes.c_char_p()
		if not core.BNGetTypePrinterTypeStringBeforeName(self.handle, type.handle, None if platform is None else platform.handle, ctypes.c_int(escaping), result_cpp):
			raise RuntimeError("BNGetTypePrinterTypeStringBeforeName returned False")

		result = core.pyNativeStr(result_cpp.value)
		core.free_string(result_cpp)
		return result

	def get_type_string_after_name(self, type: types.Type, platform: Optional[_platform.Platform] = None,
								   escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType) -> str:
		result_cpp = ctypes.c_char_p()
		if not core.BNGetTypePrinterTypeStringAfterName(self.handle, type.handle, None if platform is None else platform.handle, ctypes.c_int(escaping), result_cpp):
			raise RuntimeError("BNGetTypePrinterTypeStringAfterName returned False")

		result = core.pyNativeStr(result_cpp.value)
		core.free_string(result_cpp)
		return result

	def get_type_lines(self, type: types.Type, data: binaryview.BinaryView,
					   name: types.QualifiedNameType, line_width = 80, collapsed = False,
					   escaping: TokenEscapingType = TokenEscapingType.BackticksTokenEscapingType
					   ) -> List[types.TypeDefinitionLine]:
		if not isinstance(name, types.QualifiedName):
			name = types.QualifiedName(name)
		count = ctypes.c_ulonglong()
		core_lines = ctypes.POINTER(core.BNTypeDefinitionLine)()
		if not core.BNGetTypePrinterTypeLines(self.handle, type.handle, data.handle, name._to_core_struct(), line_width, collapsed, ctypes.c_int(escaping), core_lines, count):
			raise RuntimeError("BNGetTypePrinterTypeLines returned False")
		lines = []
		for i in range(count.value):
			tokens = _function.InstructionTextToken._from_core_struct(core_lines[i].tokens, core_lines[i].count)
			type_ = types.Type.create(handle=core.BNNewTypeReference(core_lines[i].type), platform=data.platform)
			root_type = types.Type.create(handle=core.BNNewTypeReference(core_lines[i].rootType), platform=data.platform)
			root_type_name = core.pyNativeStr(core_lines[i].rootTypeName)
			line = types.TypeDefinitionLine(core_lines[i].lineType, tokens, type_, root_type, root_type_name,
									  core_lines[i].offset, core_lines[i].fieldIndex)
			lines.append(line)
		core.BNFreeTypeDefinitionLineList(core_lines, count.value)
		return lines