|
| 1 | +# Copyright (c) 2015-2025 Vector 35 Inc |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to |
| 5 | +# deal in the Software without restriction, including without limitation the |
| 6 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 | +# sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 | +# IN THE SOFTWARE. |
| 20 | + |
| 21 | +import traceback |
| 22 | +import ctypes |
| 23 | +from typing import Optional |
| 24 | + |
| 25 | +import binaryninja |
| 26 | +from . import _binaryninjacore as core |
| 27 | +from . import function |
| 28 | +from . import enums |
| 29 | +from .log import log_error_for_exception |
| 30 | +from . import types |
| 31 | +from . import highlevelil |
| 32 | +from . import languagerepresentation |
| 33 | + |
| 34 | + |
| 35 | +class ConstantRenderer: |
| 36 | + _registered_renderers = [] |
| 37 | + renderer_name = None |
| 38 | + |
| 39 | + def __init__(self, handle=None): |
| 40 | + if handle is not None: |
| 41 | + self.handle = core.handle_of_type(handle, core.BNConstantRenderer) |
| 42 | + |
| 43 | + def register(self): |
| 44 | + if self.__class__.renderer_name is None: |
| 45 | + raise ValueError("Renderer name is missing") |
| 46 | + self._cb = core.BNCustomConstantRenderer() |
| 47 | + self._cb.context = 0 |
| 48 | + self._cb.isValidForType = self._cb.isValidForType.__class__(self._is_valid_for_type) |
| 49 | + self._cb.renderConstantPointer = self._cb.renderConstantPointer.__class__(self._render_constant_pointer) |
| 50 | + self.handle = core.BNRegisterConstantRenderer(self.__class__.renderer_name, self._cb) |
| 51 | + self.__class__._registered_renderers.append(self) |
| 52 | + |
| 53 | + def _is_valid_for_type(self, ctxt, hlil, type): |
| 54 | + try: |
| 55 | + hlil = highlevelil.HighLevelILFunction(handle=core.BNNewHighLevelILFunctionReference(hlil)) |
| 56 | + type = types.Type.create(handle=core.BNNewTypeReference(type)) |
| 57 | + return self.is_valid_for_type(hlil, type) |
| 58 | + except: |
| 59 | + log_error_for_exception("Unhandled Python exception in ConstantRenderer._is_valid_for_type") |
| 60 | + return False |
| 61 | + |
| 62 | + def _render_constant_pointer(self, ctxt, hlil, expr, type, val, tokens, settings, precedence): |
| 63 | + try: |
| 64 | + hlil = highlevelil.HighLevelILFunction(handle=core.BNNewHighLevelILFunctionReference(hlil)) |
| 65 | + type = types.Type.create(handle=core.BNNewTypeReference(type)) |
| 66 | + tokens = languagerepresentation.HighLevelILTokenEmitter(core.BNNewHighLevelILTokenEmitterReference(tokens)) |
| 67 | + if settings is not None: |
| 68 | + settings = function.DisassemblySettings(core.BNNewDisassemblySettingsReference(settings)) |
| 69 | + instr = hlil.get_expr(highlevelil.ExpressionIndex(expr)) |
| 70 | + return self.render_constant_pointer(instr, type, val, tokens, settings, precedence) |
| 71 | + except: |
| 72 | + log_error_for_exception("Unhandled Python exception in ConstantRenderer._render_constant_pointer") |
| 73 | + return False |
| 74 | + |
| 75 | + def is_valid_for_type(self, func: 'highlevelil.HighLevelILInstruction', type: 'types.Type') -> bool: |
| 76 | + return False |
| 77 | + |
| 78 | + def render_constant_pointer( |
| 79 | + self, instr: 'highlevelil.HighLevelILInstruction', type: 'types.Type', val: int, |
| 80 | + tokens: 'languagerepresentation.HighLevelILTokenEmitter', |
| 81 | + settings: Optional['function.DisassemblySettings'], precedence: 'enums.OperatorPrecedence' |
| 82 | + ) -> bool: |
| 83 | + return False |
0 commit comments