Python capstone.CsError() Examples

The following are 3 code examples of capstone.CsError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module capstone , or try the search function .
Example #1
Source File: disassembler.py    From chiasm-shell with MIT License 6 votes vote down vote up
def _set_arch(self, arch, *modes):
        """
        Try and set the current architecture
        """
        try:
            a = self.valid_archs[''.join(['CS_ARCH_', arch.upper()])]
            if a is None:
                l.error("Invalid architecture selected - run lsarch for valid options")
                return False
            ms = [self.modes[''.join(['CS_MODE_', m.upper()])] for m in modes]
        except KeyError:
            l.error("ERROR: Invalid architecture or mode string specified")
            return False
        try:
            _cs = cs.Cs(a, sum(ms))
            self._arch = (arch, modes)
            l.debug("Architecture set to %s, mode(s): %s", arch, ', '.join(modes))
            self._cs = _cs
        except cs.CsError as e:
            l.error("ERROR: %s", e)
            return False
        return True 
Example #2
Source File: disassembler.py    From chiasm-shell with MIT License 5 votes vote down vote up
def default(self, line):
        """
        Default behaviour - if no other commands are detected,
        try and disassemble the current input according to the
        currently set architecture and modes..

        :param line: Current line's text to try and disassemble.
        """
        # quick, brittle hack to enforce backslash encoding for now
        regex = re.compile('^(\\\\x[a-fA-F0-9]{2})+$')
        if not regex.match(line.strip()):
            l.error("\\xXX\\xXX... is the only valid input format (XX = hex digits)")
            return

        try:
            self._last_decoding = []
            stripped_line = re.sub(r'\\x([0-9a-fA-F]+)', r'\1', line)
            for (addr, size, mn, op_str) in \
                    self._cs.disasm_lite(binascii.a2b_hex(stripped_line), self._firstaddr):
                self._last_decoding.append((addr, size, mn, op_str))
                disas_str = "0x{:x}:\t{}\t{}".format(addr, mn, op_str)
                l.info(disas_str)
        except cs.CsError as e:
            l.error("ERROR: %s", e)
        except ValueError:
            l.error("\\xXX\\xXX... is the only valid input format (XX = hex digits)") 
Example #3
Source File: __base__.py    From deen with Apache License 2.0 5 votes vote down vote up
def unprocess(self, data):
        super(AsmBase, self).unprocess(data)
        output = ''
        try:
            for (address, size, mnemonic, op_str) in \
                    self.cs.disasm_lite(bytes(data), 0x1000):
                if len(output) > 0:
                    output += '\n'
                output += '%s\t%s' % (mnemonic, op_str)
        except capstone.CsError as e:
            self.error = e
            self.log.error(self.error)
            self.log.debug(self.error, exc_info=True)
            return b''
        return output.encode()