MCPcopy Create free account
hub / github.com/Vector35/binaryninja-api / parse_expression

Method parse_expression

python/binaryview.py:9880–9929  ·  view source on GitHub ↗

r""" Evaluates a string expression to an integer value. The parser uses the following rules: - Symbols are defined by the lexer as ``[A-Za-z0-9_:<>][A-Za-z0-9_:$\-<>]+`` or anything enclosed in either single or double quotes - Symbols are everything in ``bv.symbols``, unnamed DataVariabl

(self, expression: str, here: int = 0)

Source from the content-addressed store, hash-verified

9878 core.BNBinaryViewSetLoadSettings(self.handle, type_name, settings)
9879
9880 def parse_expression(self, expression: str, here: int = 0) -> int:
9881 r"""
9882 Evaluates a string expression to an integer value.
9883
9884 The parser uses the following rules:
9885
9886 - Symbols are defined by the lexer as ``[A-Za-z0-9_:<>][A-Za-z0-9_:$\-<>]+`` or anything enclosed in either single or double quotes
9887 - Symbols are everything in ``bv.symbols``, unnamed DataVariables (i.e. ``data_00005000``), unnamed functions (i.e. ``sub_00005000``), or section names (i.e. ``.text``)
9888 - Numbers are defaulted to hexadecimal thus `_printf + 10` is equivalent to `printf + 0x10` If decimal numbers required use the decimal prefix.
9889 - Since numbers and symbols can be ambiguous its recommended that you prefix your numbers with the following:
9890
9891 - ``0x`` - Hexadecimal
9892 - ``0n`` - Decimal
9893 - ``0`` - Octal
9894
9895 - In the case of an ambiguous number/symbol (one with no prefix) for instance ``12345`` we will first attempt
9896 to look up the string as a symbol, if a symbol is found its address is used, otherwise we attempt to convert
9897 it to a hexadecimal number.
9898 - The following operations are valid: ``+, -, \*, /, %, (), &, \|, ^, ~, ==, !=, >, <, >=, <=``
9899
9900 - Comparison operators return 1 if the condition is true, 0 otherwise.
9901
9902 - In addition to the above operators there are dereference operators similar to BNIL style IL:
9903
9904 - ``[<expression>]`` - read the `current address size` at ``<expression>``
9905 - ``[<expression>].b`` - read the byte at ``<expression>``
9906 - ``[<expression>].w`` - read the word (2 bytes) at ``<expression>``
9907 - ``[<expression>].d`` - read the dword (4 bytes) at ``<expression>``
9908 - ``[<expression>].q`` - read the quadword (8 bytes) at ``<expression>``
9909
9910 - The ``$here`` (or more succinctly: ``$``) keyword can be used in calculations and is defined as the ``here`` parameter, or the currently selected address
9911 - The ``$start``/``$end`` keyword represents the address of the first/last bytes in the file respectively
9912 - Arbitrary magic values (name-value-pairs) can be added to the expression parser via the
9913 :py:func:`add_expression_parser_magic_value` API. Notably, the debugger adds all register values into the
9914 expression parser so they can be used directly when navigating. The register values can be referenced like
9915 `$rbp`, `$x0`, etc. For more details, refer to the related
9916 `debugger docs <https://docs.binary.ninja/guide/debugger/index.html#navigating-the-binary>`_.
9917
9918 :param str expression: Arithmetic expression to be evaluated
9919 :param int here: (optional) Base address for relative expressions, defaults to zero
9920 :rtype: int
9921 """
9922 offset = ctypes.c_ulonglong()
9923 errors = ctypes.c_char_p()
9924 if not core.BNParseExpression(self.handle, expression, offset, here, errors):
9925 assert errors.value is not None, "core.BNParseExpression returned errors set to None"
9926 error_str = errors.value.decode("utf-8")
9927 core.free_string(errors)
9928 raise ValueError(error_str)
9929 return offset.value
9930
9931 def eval(self, expression: str, here: int = 0) -> int:
9932 """

Callers 4

evalMethod · 0.95
_set_hereFunction · 0.80
_set_current_raw_offsetFunction · 0.80
_set_current_selectionFunction · 0.80

Calls 1

decodeMethod · 0.45

Tested by

no test coverage detected