Exemplo n.º 1
0
    def paint(self, painter, option, idx):
        # Draw background highlight in theme style
        selected = option.state & QStyle.State_Selected != 0
        if selected:
            painter.setBrush(
                binaryninjaui.getThemeColor(
                    binaryninjaui.ThemeColor.SelectionColor))
        else:
            painter.setBrush(option.backgroundBrush)
        painter.setPen(Qt.NoPen)
        painter.drawRect(option.rect)

        text = idx.data()
        state = idx.data(Qt.UserRole)

        # Draw text depending on state
        painter.setFont(self.font)
        if state == 'updated':
            painter.setPen(option.palette.color(QPalette.Highlight).rgba())
        elif state == 'modified':
            painter.setPen(
                binaryninjaui.getThemeColor(
                    ThemeColor.OrangeStandardHighlightColor).rgba())
        else:
            painter.setPen(option.palette.color(QPalette.WindowText).rgba())
        painter.drawText(2 + option.rect.left(),
                         self.char_offset + self.baseline + option.rect.top(),
                         str(text))
Exemplo n.º 2
0
	def run(self):
		width = self.image.width()
		for i in range(0, width):
			v = int(self.data.get_entropy(self.data.start + i * self.block_size, self.block_size)[0] * 255)
			if v >= 240:
				color = binaryninjaui.getThemeColor(ThemeColor.YellowStandardHighlightColor)
				self.image.setPixelColor(i, 0, color)
			else:
				baseColor = binaryninjaui.getThemeColor(ThemeColor.FeatureMapBaseColor)
				entropyColor = binaryninjaui.getThemeColor(ThemeColor.BlueStandardHighlightColor)
				color = binaryninjaui.mixColor(baseColor, entropyColor, v)
				self.image.setPixelColor(i, 0, color)
			self.updated = True
Exemplo n.º 3
0
	def paint(self, painter, option, idx):
		# Draw background highlight in theme style
		selected = option.state & QStyle.State_Selected != 0
		if selected:
			painter.setBrush(binaryninjaui.getThemeColor(binaryninjaui.ThemeColor.SelectionColor))
		else:
			painter.setBrush(option.backgroundBrush)
		painter.setPen(Qt.NoPen)
		painter.drawRect(option.rect)

		text = idx.data()
		painter.setFont(self.font)
		painter.setPen(option.palette.color(QPalette.WindowText).rgba())
		painter.drawText(2 + option.rect.left(), self.char_offset + self.baseline + option.rect.top(), str(text))
Exemplo n.º 4
0
 def __init__(self, text):
     super(ClickableCodeLabel, self).__init__(
         text, binaryninjaui.getThemeColor(ThemeColor.CodeSymbolColor),
         self.clickEvent)
     self.address = int(text, 0)
Exemplo n.º 5
0
    def paintEvent(self, event):
        p = QPainter(self.viewport())
        render = self.createRenderContext()
        render.init(p)
        charWidth = render.getFontWidth()
        charHeight = render.getFontHeight()

        # Compute range that needs to be updated
        topY = event.rect().y()
        botY = topY + event.rect().height()
        topY = (topY - 2) // charHeight
        botY = ((botY - 2) // charHeight) + 1

        # Compute selection range
        selection = False
        selStart, selEnd = self.getSelectionOffsets()
        if selStart != selEnd:
            selection = True

        # Draw selection
        if selection:
            startY = None
            endY = None
            startX = None
            endX = None
            for i in range(0, len(self.lines)):
                if selStart >= self.lines[i].address:
                    startY = i - self.topLine
                    startX = selStart - self.lines[i].address
                    if startX > self.cols:
                        startX = self.cols
                if selEnd >= self.lines[i].address:
                    endY = i - self.topLine
                    endX = selEnd - self.lines[i].address
                    if endX > self.cols:
                        endX = self.cols

            if startY is not None and endY is not None:
                p.setPen(binaryninjaui.getThemeColor(
                    ThemeColor.SelectionColor))
                p.setBrush(
                    binaryninjaui.getThemeColor(ThemeColor.SelectionColor))
                if startY == endY:
                    p.drawRect(2 + (self.addrWidth + 2 + startX) * charWidth,
                               2 + startY * charHeight,
                               (endX - startX) * charWidth, charHeight + 1)
                else:
                    p.drawRect(2 + (self.addrWidth + 2 + startX) * charWidth,
                               2 + startY * charHeight,
                               (self.cols - startX) * charWidth,
                               charHeight + 1)
                    if endX > 0:
                        p.drawRect(2 + (self.addrWidth + 2) * charWidth,
                                   2 + endY * charHeight, endX * charWidth,
                                   charHeight + 1)
                if (endY - startY) > 1:
                    p.drawRect(2 + (self.addrWidth + 2) * charWidth,
                               2 + (startY + 1) * charHeight,
                               self.cols * charWidth,
                               ((endY - startY) - 1) * charHeight + 1)

        # Paint each line
        color = self.palette().color(QPalette.WindowText)
        for y in range(topY, botY):
            if (y + self.topLine) < 0:
                continue
            if (y + self.topLine) >= len(self.lines):
                break
            if self.lines[y + self.topLine].separator:
                render.drawLinearDisassemblyLineBackground(
                    p,
                    LinearDisassemblyLineType.NonContiguousSeparatorLineType,
                    QRect(0, 2 + y * charHeight,
                          event.rect().width(), charHeight), 0)
                continue

            lineStartAddr = self.lines[y + self.topLine].address
            addrStr = "%.8x" % lineStartAddr
            length = self.lines[y + self.topLine].length
            text = self.lines[y + self.topLine].text

            cursorCol = None
            if (((self.cursorAddr >= lineStartAddr) and
                 (self.cursorAddr < (lineStartAddr + length)))
                    or (((y + self.topLine + 1) >= len(self.lines)) and
                        (self.cursorAddr == (lineStartAddr + length)))):
                cursorCol = self.cursorAddr - lineStartAddr

            render.drawText(
                p, 2, 2 + y * charHeight,
                binaryninjaui.getThemeColor(ThemeColor.AddressColor), addrStr)
            render.drawText(p, 2 + (self.addrWidth + 2) * charWidth,
                            2 + y * charHeight, color, text)

            if self.caretVisible and self.caretBlink and not selection and cursorCol is not None:
                p.setPen(Qt.NoPen)
                p.setBrush(self.palette().color(QPalette.WindowText))
                p.drawRect(2 + (self.addrWidth + 2 + cursorCol) * charWidth,
                           2 + y * charHeight, charWidth, charHeight + 1)
                caretTextColor = self.palette().color(QPalette.Base)
                byteValue = self.data.read(lineStartAddr + cursorCol, 1)
                if len(byteValue) == 1:
                    byteStr = self.byte_mapping[byteValue[0]]
                    render.drawText(
                        p, 2 + (self.addrWidth + 2 + cursorCol) * charWidth,
                        2 + y * charHeight, caretTextColor, byteStr)
Exemplo n.º 6
0
 def highlightBlock(self, text):
     # Highlight keywords
     keywords = ["(?<=\\W\\()\\w+(?=[ *&]*\\))","\\w+ (?=.*\\)$)","^ +\\w+ (?=[*&_A-Za-z0-9\\[\\] ]+;)","^\w+","\\bin_addr\\b","\\bssize_t\\b","\\bsocklen_t\\b","\\bsa_family_t\\b","\\b__int32_t\\b","\\b__int8_t\\b","\\b__int16_t\\b","\\b__uint32_t\\b","\\b__uint8_t\\b","\\b__uint16_t\\b","\\bpid_t\\b","\\bcode\\b","\\bLPSTR\\b","\\bSIZE_T\\b","\\bLPVOID\\b","\\bDWORD\\b","\\bclock_t\\b","\\bthis\\b","\\bUINT\\b","\\bHANDLE\\b","\\blonglong\\b","\\bushort\\b","\\bFILE\\b","\\bulong\\b","\\bbyte\\b","\\bfalse\\b","\\btrue\\b","\\buint\\b","\\bsize_t\\b","\\bundefined\\d*\\b","\\bchar\\b", "\\bclass\\b", "\\bconst\\b", "\\bdouble\\b", "\\benum\\b", "\\bexplicit\\b","\\bfriend\\b", "\\binline\\b", "\\bint\\b","\\blong\\b", "\\bnamespace\\b", "\\boperator\\b","\\bprivate\\b", "\\bprotected\\b", "\\bpublic\\b","\\bshort\\b", "\\bsignals\\b", "\\bsigned\\b","\\bslots\\b", "\\bstatic\\b", "\\bstruct\\b","\\btemplate\\b", "\\btypedef\\b", "\\btypename\\b","\\bunion\\b", "\\bunsigned\\b", "\\bvirtual\\b","\\bvoid\\b", "\\bvolatile\\b", "\\bbool\\b"]
     keyword_format = QTextCharFormat()
     keyword_format.setForeground(getThemeColor(enums.ThemeColor.KeywordColor))
     for keyword in keywords:
         for match in re.finditer(keyword, text):
             self.setFormat(match.start(), match.end() - match.start(), keyword_format)
     # Highlight flow words
     flow_words = ["\\breturn\\b","\\bif\\b","\\belse\\b","\\bswitch\\b","\\bcase\\b","\\bwhile\\b","\\bfor\\b","\\bdo\\b","\\bgoto\\b"]
     flow_format = QTextCharFormat()
     flow_format.setForeground(getThemeColor(enums.ThemeColor.TokenHighlightColor))
     for flow in flow_words:
         for match in re.finditer(flow, text):
             self.setFormat(match.start(), match.end() - match.start(), flow_format)
     # Highlight functions
     function_format = QTextCharFormat()
     function_format.setForeground(getThemeColor(enums.ThemeColor.CodeSymbolColor))
     function_pattern = "\\b\\w+(?=\\()"
     for match in re.finditer(function_pattern, text):
         self.setFormat(match.start(), match.end() - match.start(), function_format)
     # Highlight comments
     comment_format = QTextCharFormat()
     comment_format.setForeground(getThemeColor(enums.ThemeColor.CommentColor))
     comment_pattern = "\/\/.*$"
     for match in re.finditer(comment_pattern, text):
         self.setFormat(match.start(), match.end() - match.start(), comment_format)
     multi_comment_pattern = "(?s)\\/\\*.*?\\*\\/"
     for match in re.finditer(multi_comment_pattern, text):
         self.setFormat(match.start(), match.end() - match.start(), comment_format)
     # Highlight string constants
     const_format = QTextCharFormat()
     const_format.setForeground(getThemeColor(enums.ThemeColor.StringColor))
     string_consts = "\"(.*?)\""
     for match in re.finditer(string_consts, text):
         self.setFormat(match.start(), match.end() - match.start(), const_format)
     # Highlight numeric constants
     num_const_format = QTextCharFormat()
     num_const_format.setForeground(getThemeColor(enums.ThemeColor.NumberColor))
     num_consts = "\\b\\d+\\b"
     for match in re.finditer(num_consts, text):
         self.setFormat(match.start(), match.end() - match.start(), num_const_format)
     hex_const = "0x[0-9a-f]+\\b"
     for match in re.finditer(hex_const, text):
         self.setFormat(match.start(), match.end() - match.start(), num_const_format)
     # Highlight data
     data_format = QTextCharFormat()
     data_format.setForeground(getThemeColor(enums.ThemeColor.DataSymbolColor))
     data_consts = "\\b(PTR)?_?DAT_[0-9a-zA-Z]+\\b"
     for match in re.finditer(data_consts, text):
         self.setFormat(match.start(), match.end() - match.start(), data_format)
     # Highlight CPP Class paths
     cpp_format = QTextCharFormat()
     cpp_format.setForeground(getThemeColor(enums.ThemeColor.NameSpaceColor))
     cpp_path = "\\b\\w*(?=::)"
     for match in re.finditer(cpp_path, text):
         self.setFormat(match.start(), match.end() - match.start(), cpp_format)
     # Params
     params_format = QTextCharFormat()
     params_format.setForeground(getThemeColor(enums.ThemeColor.FieldNameColor))
     for arg in self.args:
         params_pattern = "\\b" + arg + "\\b"
         for match in re.finditer(params_pattern, text):
             self.setFormat(match.start(), match.end() - match.start(), params_format)
     # Highlight selection
     if self.selected:
         selection_format = QTextCharFormat()
         #selection_format.setBackground(getThemeColor(enums.ThemeColor.Highlight))
         selection_format.setBackground(QColor.fromRgb(121,195,231))
         selection_format.setForeground(QColor.fromRgb(42,42,42))
         try:
             selection_pattern = self.selected
             for match in re.finditer(selection_pattern, text):
                 self.setFormat(match.start(), match.end() - match.start(), selection_format)
         except:
             pass
Exemplo n.º 7
0
        _format.setFontItalic(True)

    return _format


STYLES = {
    'keyword': format('StackVariableColor'),
    'operator': format('TokenHighlightColor'),
    'brace': format('LinearDisassemblySeparatorColor'),
    'defclass': format('DataSymbolColor'),
    'string': format('StringColor'),
    'string2': format('TypeNameColor'),
    'comment': format('AnnotationColor', 'italic'),
    'self': format('KeywordColor', 'italic'),
    'numbers': format('NumberColor'),
    'numberbar': getThemeColor(ThemeColor.BackgroundHighlightDarkColor),
    'blockselected': getThemeColor(ThemeColor.TokenHighlightColor),
    'blocknormal': getThemeColor(ThemeColor.TokenSelectionColor)
}


class PythonHighlighter(QSyntaxHighlighter):
    """Syntax highlighter for the Python language.
	"""
    # Python keywords
    keywords = [
        'and',
        'assert',
        'break',
        'class',
        'continue',