Exemple #1
0
    def _parse_types_from_source(self, ctxt, source, fileName, platform_,
                                 existingTypes, existingTypeCount, options,
                                 optionCount, includeDirs, includeDirCount,
                                 autoTypeSource, result, errors,
                                 errorCount) -> bool:
        try:
            source_py = core.pyNativeStr(source)
            file_name_py = core.pyNativeStr(fileName)
            platform_py = platform.Platform(
                handle=core.BNNewPlatformReference(platform_))

            existing_types_py = []
            for i in range(existingTypeCount):
                existing_types_py.append(
                    QualifiedNameTypeAndId._from_core_struct(existingTypes[i]))

            options_py = []
            for i in range(optionCount):
                options_py.append(core.pyNativeStr(options[i]))

            include_dirs_py = []
            for i in range(includeDirCount):
                include_dirs_py.append(core.pyNativeStr(includeDirs[i]))

            auto_type_source = core.pyNativeStr(autoTypeSource)

            (result_py, errors_py) = self.parse_types_from_source(
                source_py, file_name_py, platform_py, existing_types_py,
                options_py, include_dirs_py, auto_type_source)

            if result_py is not None and result is not None:
                result_struct = result_py._to_core_struct()
                TypeParser._cached_result = result_struct
                result[0] = result_struct

            if errorCount is not None:
                errorCount[0] = len(errors_py)
            if errors is not None:
                errors_out = (core.BNTypeParserError * len(errors_py))()
                for i in range(len(errors_py)):
                    errors_out[i] = errors_py[i]._to_core_struct()
                TypeParser._cached_error = errors_out
                errors[0] = errors_out

            return result_py is not None
        except:
            result[0].typeCount = 0
            result[0].variableCount = 0
            result[0].functionCount = 0
            errorCount[0] = 0
            log_error(traceback.format_exc())
            return False
def authentication_methods() -> List[Tuple[str, str]]:
    """
	Get a list of authentication methods accepted by the Enterprise Server.
	:return: List of (<method name>, <method display name>) tuples
	"""
    if not is_connected():
        connect()
    methods = ctypes.POINTER(ctypes.c_char_p)()
    names = ctypes.POINTER(ctypes.c_char_p)()
    count = core.BNGetEnterpriseServerAuthenticationMethods(methods, names)
    results = []
    for i in range(count):
        results.append(
            (core.pyNativeStr(methods[i]), core.pyNativeStr(names[i])))
    core.BNFreeStringList(methods, count)
    core.BNFreeStringList(names, count)
    return results
Exemple #3
0
    def _preprocess_source(self, ctxt, source, fileName, platform_,
                           existingTypes, existingTypeCount, options,
                           optionCount, includeDirs, includeDirCount, output,
                           errors, errorCount) -> bool:
        try:
            source_py = core.pyNativeStr(source)
            file_name_py = core.pyNativeStr(fileName)
            platform_py = platform.Platform(
                handle=core.BNNewPlatformReference(platform_))

            existing_types_py = []
            for i in range(existingTypeCount):
                existing_types_py.append(
                    QualifiedNameTypeAndId._from_core_struct(existingTypes[i]))

            options_py = []
            for i in range(optionCount):
                options_py.append(core.pyNativeStr(options[i]))

            include_dirs_py = []
            for i in range(includeDirCount):
                include_dirs_py.append(core.pyNativeStr(includeDirs[i]))

            (output_py, errors_py) = self.preprocess_source(
                source_py, file_name_py, platform_py, existing_types_py,
                options_py, include_dirs_py)

            if output_py is not None and output is not None:
                TypeParser._cached_string = core.cstr(output_py)
                output[0] = TypeParser._cached_string
            if errorCount is not None:
                errorCount[0] = len(errors_py)
            if errors is not None:
                errors_out = (core.BNTypeParserError * len(errors_py))()
                for i in range(len(errors_py)):
                    errors_out[i] = errors_py[i]._to_core_struct()
                TypeParser._cached_error = errors_out
                errors[0] = errors_out

            return output_py is not None
        except:
            errorCount[0] = 0
            log_error(traceback.format_exc())
            return False
Exemple #4
0
    def preprocess_source(
        self,
        source: str,
        file_name: str,
        platform: 'platform.Platform',
        existing_types: Optional[List[QualifiedNameTypeAndId]] = None,
        options: Optional[List[str]] = None,
        include_dirs: Optional[List[str]] = None
    ) -> Tuple[Optional[str], List[TypeParserError]]:
        if existing_types is None:
            existing_types = []
        if options is None:
            options = []
        if include_dirs is None:
            include_dirs = []

        existing_types_cpp = (core.BNQualifiedNameTypeAndId *
                              len(existing_types))()
        for (i, qnatid) in enumerate(existing_types):
            existing_types_cpp[i] = qnatid._to_core_struct()

        options_cpp = (ctypes.c_char_p * len(options))()
        for (i, s) in enumerate(options):
            options_cpp[i] = core.cstr(s)

        include_dirs_cpp = (ctypes.c_char_p * len(include_dirs))()
        for (i, s) in enumerate(include_dirs):
            include_dirs_cpp[i] = core.cstr(s)

        output_cpp = ctypes.c_char_p()
        errors_cpp = ctypes.POINTER(core.BNTypeParserError)()
        error_count = ctypes.c_size_t()

        success = core.BNTypeParserPreprocessSource(
            self.handle,
            source, file_name, platform.handle, existing_types_cpp,
            len(existing_types), options_cpp, len(options), include_dirs_cpp,
            len(include_dirs), output_cpp, errors_cpp, error_count)

        if success:
            output = core.pyNativeStr(output_cpp.value)
            core.free_string(output_cpp)
        else:
            output = None

        errors = []
        for i in range(error_count.value):
            errors.append(TypeParserError._from_core_struct(errors_cpp[i]))
        core.BNFreeTypeParserErrors(errors_cpp, error_count.value)

        return output, errors
    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_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_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
Exemple #8
0
    def _parse_type_string(self, ctxt, source, platform_, existingTypes,
                           existingTypeCount, result, errors,
                           errorCount) -> bool:
        try:
            source_py = core.pyNativeStr(source)
            platform_py = platform.Platform(
                handle=core.BNNewPlatformReference(platform_))

            existing_types_py = []
            for i in range(existingTypeCount):
                existing_types_py.append(
                    QualifiedNameTypeAndId._from_core_struct(existingTypes[i]))

            (result_py,
             errors_py) = self.parse_type_string(source_py, platform_py,
                                                 existing_types_py)

            if result_py is not None and result is not None:
                result[0].name = types.QualifiedName(
                    result_py[0])._to_core_struct()
                result[0].type = core.BNNewTypeReference(result_py[1].handle)

            if errorCount is not None:
                errorCount[0] = len(errors_py)
            if errors is not None:
                errors_out = (core.BNTypeParserError * len(errors_py))()
                for i in range(len(errors_py)):
                    errors_out[i] = errors_py[i]._to_core_struct()
                TypeParser._cached_error = errors_out
                errors[0] = errors_out

            return result_py is not None
        except:
            errorCount[0] = 0
            log_error(traceback.format_exc())
            return False