Пример #1
0
    def get_string(self, addr, max_data_size=-1, s=None):
        if s is None:
            s = self.get_section(addr)
            if s is None:
                return None

        data = s.data
        off = addr - s.start
        txt = []

        c = 0
        i = 0
        while (i < max_data_size or max_data_size == -1) and off < len(data):
            c = data[off]
            if c == 0:
                break
            if c not in BYTES_PRINTABLE_SET:
                break
            txt.append(get_char(c))
            off += 1
            i += 1

        if i == max_data_size:
            if c != 0:
                txt.append("...")
        elif c != 0 or i == 0:
            return None

        return ''.join(txt)
Пример #2
0
    def get_string(self, addr, max_data_size=-1, s=None):
        if s is None:
            s = self.get_section(addr)
            if s is None:
                return None

        data = s.data
        off = addr - s.start
        txt = []

        c = 0
        i = 0
        while (i < max_data_size or max_data_size == -1) and off < len(data):
            c = data[off]
            if c == 0:
                break
            if c not in BYTES_PRINTABLE_SET:
                break
            txt.append(get_char(c))
            off += 1
            i += 1

        if i == max_data_size:
            if c != 0:
                txt.append("...")
        elif c != 0 or i == 0:
            return None

        return ''.join(txt)
Пример #3
0
    def _imm(self,
             imm,
             op_size,
             hexa,
             section=None,
             print_data=True,
             force_dont_print_data=False,
             is_from_jump=False):

        if self.gctx.capstone_string != 0:
            hexa = True

        if hexa:
            imm = unsigned(imm)

        label_printed = self._label(imm, print_colon=False)

        if label_printed:
            ty = self._dis.mem.get_type(imm)
            # ty == -1 : from the terminal (with -x) there are no xrefs if
            # the file was loaded without a database.
            if ty == MEM_HEAD and self._dis.mem.get_type(
                    self._dis.mem.get_head_addr(imm)) == MEM_ASCII:
                ty = MEM_ASCII

            if imm in self._dis.xrefs and ty != MEM_UNK and \
                    ty != MEM_ASCII or ty == -1:
                return

            if ty == MEM_ASCII:
                print_data = True
                force_dont_print_data = False

        if section is None:
            section = self._binary.get_section(imm)

        if section is not None and section.start == 0:
            section = None

        # For a raw file, if the raw base is 0 the immediate is considered
        # as an address only if it's in the symbols list.
        raw_base_zero = self._binary.type == T_BIN_RAW and self.gctx.raw_base == 0

        if section is not None and not raw_base_zero:
            if not label_printed:
                self._address(imm, print_colon=False, notprefix=True)

            if not force_dont_print_data and print_data:
                s = self._binary.get_string(imm, self.gctx.max_data_size)
                if s is not None:
                    s = s.replace("\n", "\\n")
                    self._add(" ")
                    self._string('"' + s + '"')

            return

        if label_printed:
            return

        if op_size == 1:
            if imm == 10:
                self._string("'\\n'")
            else:
                self._string("'%s'" % get_char(imm))
        elif hexa:
            if is_from_jump:
                self._error(hex(imm))
            else:
                self._add(hex(imm))
        else:
            if op_size == 4:
                self._add(str(c_int(imm).value))
            elif op_size == 2:
                self._add(str(c_short(imm).value))
            else:
                self._add(str(c_long(imm).value))

            if imm > 0:
                if op_size == 4:
                    packed = struct.pack("<L", imm)
                elif op_size == 8:
                    packed = struct.pack("<Q", imm)
                else:
                    return
                if set(packed).issubset(BYTES_PRINTABLE_SET):
                    self._string(" \"" + "".join(map(chr, packed)) + "\"")

        return
Пример #4
0
    def _imm(self, imm, op_size, hexa, section=None, print_data=True,
             force_dont_print_data=False, is_from_jump=False):

        if self.gctx.capstone_string != 0:
            hexa = True

        if hexa:
            imm = unsigned(imm)

        label_printed = self._label(imm, print_colon=False)

        if label_printed:
            ty = self._dis.mem.get_type(imm)
            # ty == -1 : from the terminal (with -x) there are no xrefs if
            # the file was loaded without a database.
            if ty == MEM_HEAD and self._dis.mem.get_type(
                    self._dis.mem.get_head_addr(imm)) == MEM_ASCII:
                ty = MEM_ASCII

            if imm in self._dis.xrefs and ty != MEM_UNK and \
                    ty != MEM_ASCII or ty == -1:
                return

            if ty == MEM_ASCII:
                print_data = True
                force_dont_print_data = False

        if section is None:
            section = self._binary.get_section(imm)

        if section is not None and section.start == 0:
            section = None

        # For a raw file, if the raw base is 0 the immediate is considered
        # as an address only if it's in the symbols list.
        raw_base_zero = self._binary.type == T_BIN_RAW and self.gctx.raw_base == 0

        if section is not None and not raw_base_zero:
            if not label_printed:
                self._address(imm, print_colon=False, notprefix=True)

            if not force_dont_print_data and print_data:
                s = self._binary.get_string(imm, self.gctx.max_data_size)
                if s is not None:
                    s = s.replace("\n", "\\n")
                    self._add(" ")
                    self._string('"' + s + '"')

            return

        if label_printed:
            return

        if op_size == 1:
            if imm == 10:
                self._string("'\\n'")
            else:
                self._string("'%s'" % get_char(imm))
        elif hexa:
            if is_from_jump:
                self._error(hex(imm))
            else:
                self._add(hex(imm))
        else:
            if op_size == 4:
                self._add(str(c_int(imm).value))
            elif op_size == 2:
                self._add(str(c_short(imm).value))
            else:
                self._add(str(c_long(imm).value))

            if imm > 0:
                if op_size == 4:
                    packed = struct.pack("<L", imm)
                elif op_size == 8:
                    packed = struct.pack("<Q", imm)
                else:
                    return
                if set(packed).issubset(BYTES_PRINTABLE_SET):
                    self._string(" \"" + "".join(map(chr, packed)) + "\"")

        return