First try with colors

This commit is contained in:
xarkes 2017-10-15 09:14:05 +02:00
parent 9c487b63ca
commit 1ca9b06519
6 changed files with 158 additions and 11 deletions

View File

@ -76,7 +76,8 @@ SOURCES += \
widgets/DisassemblyWidget.cpp \ widgets/DisassemblyWidget.cpp \
widgets/SidebarWidget.cpp \ widgets/SidebarWidget.cpp \
widgets/HexdumpWidget.cpp \ widgets/HexdumpWidget.cpp \
utils/Configuration.cpp utils/Configuration.cpp \
utils/Colors.cpp
HEADERS += \ HEADERS += \
cutter.h \ cutter.h \
@ -124,7 +125,8 @@ HEADERS += \
widgets/DisassemblyWidget.h \ widgets/DisassemblyWidget.h \
widgets/SidebarWidget.h \ widgets/SidebarWidget.h \
widgets/HexdumpWidget.h \ widgets/HexdumpWidget.h \
utils/Configuration.h utils/Configuration.h \
utils/Colors.h
FORMS += \ FORMS += \
widgets/PreviewWidget.ui \ widgets/PreviewWidget.ui \

88
src/utils/Colors.cpp Normal file
View File

@ -0,0 +1,88 @@
#include "Colors.h"
Colors::Colors()
{
}
// Temporary solution
// Copied from R_API const char* r_print_color_op_type(RPrint *p, ut64 anal_type) {
QString Colors::getColor(ut64 type)
{
switch (type & R_ANAL_OP_TYPE_MASK) {
case R_ANAL_OP_TYPE_NOP:
return "nop";
case R_ANAL_OP_TYPE_ADD:
case R_ANAL_OP_TYPE_SUB:
case R_ANAL_OP_TYPE_MUL:
case R_ANAL_OP_TYPE_DIV:
case R_ANAL_OP_TYPE_MOD:
case R_ANAL_OP_TYPE_LENGTH:
return "math";
case R_ANAL_OP_TYPE_AND:
case R_ANAL_OP_TYPE_OR:
case R_ANAL_OP_TYPE_XOR:
case R_ANAL_OP_TYPE_NOT:
case R_ANAL_OP_TYPE_SHL:
case R_ANAL_OP_TYPE_SAL:
case R_ANAL_OP_TYPE_SAR:
case R_ANAL_OP_TYPE_SHR:
case R_ANAL_OP_TYPE_ROL:
case R_ANAL_OP_TYPE_ROR:
case R_ANAL_OP_TYPE_CPL:
return "bin";
case R_ANAL_OP_TYPE_IO:
return "swi";
case R_ANAL_OP_TYPE_JMP:
case R_ANAL_OP_TYPE_UJMP:
case R_ANAL_OP_TYPE_IJMP:
case R_ANAL_OP_TYPE_RJMP:
case R_ANAL_OP_TYPE_IRJMP:
case R_ANAL_OP_TYPE_MJMP:
return "jmp";
case R_ANAL_OP_TYPE_CJMP:
case R_ANAL_OP_TYPE_UCJMP:
case R_ANAL_OP_TYPE_SWITCH:
return "cjmp";
case R_ANAL_OP_TYPE_CMP:
case R_ANAL_OP_TYPE_ACMP:
return "cmp";
case R_ANAL_OP_TYPE_UCALL:
case R_ANAL_OP_TYPE_ICALL:
case R_ANAL_OP_TYPE_RCALL:
case R_ANAL_OP_TYPE_IRCALL:
case R_ANAL_OP_TYPE_UCCALL:
case R_ANAL_OP_TYPE_CALL:
case R_ANAL_OP_TYPE_CCALL:
return "call";
case R_ANAL_OP_TYPE_NEW:
case R_ANAL_OP_TYPE_SWI:
return "swi";
case R_ANAL_OP_TYPE_ILL:
case R_ANAL_OP_TYPE_TRAP:
return "trap";
case R_ANAL_OP_TYPE_CRET:
case R_ANAL_OP_TYPE_RET:
return "ret";
case R_ANAL_OP_TYPE_CAST:
case R_ANAL_OP_TYPE_MOV:
case R_ANAL_OP_TYPE_LEA:
case R_ANAL_OP_TYPE_CMOV: // TODO: add cmov cathegory?
return "mov";
case R_ANAL_OP_TYPE_PUSH:
case R_ANAL_OP_TYPE_UPUSH:
case R_ANAL_OP_TYPE_LOAD:
return "push";
case R_ANAL_OP_TYPE_POP:
case R_ANAL_OP_TYPE_STORE:
return "pop";
case R_ANAL_OP_TYPE_CRYPTO:
return "crypto";
case R_ANAL_OP_TYPE_NULL:
return "other";
case R_ANAL_OP_TYPE_UNK:
default:
return "invalid";
}
}

14
src/utils/Colors.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef COLORS_H
#define COLORS_H
#include "cutter.h"
#include "libr/r_anal.h"
class Colors
{
public:
Colors();
static QString getColor(ut64 type);
};
#endif // COLORS_H

View File

@ -1,10 +1,14 @@
#include "Configuration.h" #include "Configuration.h"
#include <QJsonObject>
#include <QJsonArray>
Configuration* Configuration::mPtr = nullptr; Configuration* Configuration::mPtr = nullptr;
Configuration::Configuration() : QObject() Configuration::Configuration() : QObject()
{ {
mPtr = this; mPtr = this;
loadDefaultColors();
} }
Configuration* Configuration::instance() Configuration* Configuration::instance()
@ -12,6 +16,18 @@ Configuration* Configuration::instance()
return mPtr; return mPtr;
} }
void Configuration::loadDefaultColors()
{
Core()->cmd("eco solarized");
QJsonObject colors = Core()->cmdj("ecj").object();
for (auto color : colors.keys()) {
QJsonArray rgb = colors[color].toArray();
QColor col = QColor(rgb[0].toInt(), rgb[1].toInt(), rgb[2].toInt());
s.setValue("colors." + color, col);
}
s.setValue("colors.gui.alt_background", QColor(255, 255, 255));
}
const QFont Configuration::getFont() const const QFont Configuration::getFont() const
{ {
QFont font = s.value("font", QFont("Monospace", 12)).value<QFont>(); QFont font = s.value("font", QFont("Monospace", 12)).value<QFont>();
@ -23,3 +39,12 @@ void Configuration::setFont(const QFont &font)
s.setValue("font", font); s.setValue("font", font);
emit fontsUpdated(); emit fontsUpdated();
} }
const QColor Configuration::getColor(const QString &name) const
{
if (s.contains("colors." + name)) {
return s.value("colors." + name).value<QColor>();
} else {
return s.value("colors.other").value<QColor>();
}
}

View File

@ -3,8 +3,10 @@
#include <QSettings> #include <QSettings>
#include <QFont> #include <QFont>
#include <cutter.h>
#define Config() (Configuration::instance()) #define Config() (Configuration::instance())
#define ConfigColor(x) Config()->getColor(x)
class Configuration : public QObject class Configuration : public QObject
{ {
@ -13,6 +15,8 @@ private:
QSettings s; QSettings s;
static Configuration* mPtr; static Configuration* mPtr;
void loadDefaultColors();
public: public:
// Functions // Functions
Configuration(); Configuration();
@ -22,6 +26,9 @@ public:
const QFont getFont() const; const QFont getFont() const;
void setFont(const QFont &font); void setFont(const QFont &font);
// Colors
const QColor getColor(const QString &name) const;
// TODO Imho it's wrong doing it this way. Should find something else. // TODO Imho it's wrong doing it this way. Should find something else.
bool getAsmESIL() const { return s.value("asm.esil", false).toBool(); } bool getAsmESIL() const { return s.value("asm.esil", false).toBool(); }
void setAsmESIL(bool v) { s.setValue("asm.esil", v); } void setAsmESIL(bool v) { s.setValue("asm.esil", v); }

View File

@ -10,6 +10,7 @@
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
#include "utils/Configuration.h" #include "utils/Configuration.h"
#include "utils/Colors.h"
#ifdef _WIN32 #ifdef _WIN32
#undef min #undef min
@ -1592,20 +1593,27 @@ void DisassemblerGraphView::loadCurrentGraph()
// TODO // TODO
RichTextPainter::List richText; RichTextPainter::List richText;
////////// TODO Put in a helper function to use it on DisassemblyWidget too.
RichTextPainter::CustomRichText_t assembly; RichTextPainter::CustomRichText_t assembly;
assembly.highlight = false; assembly.highlight = false;
assembly.flags = RichTextPainter::FlagColor; assembly.flags = RichTextPainter::FlagColor;
assembly.text = op["opcode"].toString(); // TODO cut opcode and use op["ptr"] to colorate registers and immediate values
QString opcode = op["opcode"].toString();
assembly.text = opcode;
QString colorName = Colors::getColor(op["type_num"].toVariant().toULongLong());
assembly.textColor = ConfigColor(colorName);
richText.insert(richText.begin(), assembly); richText.insert(richText.begin(), assembly);
if (op["comment"].toString().length()) { if (op["comment"].toString().length()) {
RichTextPainter::CustomRichText_t comment; RichTextPainter::CustomRichText_t comment;
comment.text = QString(" ; %1").arg(QByteArray::fromBase64(op["comment"].toString().toLocal8Bit()).data()); comment.text = QString(" ; %1").arg(QByteArray::fromBase64(op["comment"].toString().toLocal8Bit()).data());
comment.textColor = Qt::blue; comment.textColor = ConfigColor("comment");
comment.flags = RichTextPainter::FlagColor; comment.flags = RichTextPainter::FlagColor;
richText.insert(richText.end(), comment); richText.insert(richText.end(), comment);
} }
//////////
i.text = Text(richText); i.text = Text(richText);
b.instrs.push_back(i); b.instrs.push_back(i);
@ -1618,6 +1626,9 @@ void DisassemblerGraphView::loadCurrentGraph()
this->analysis = anal; this->analysis = anal;
this->function = this->analysis.entry; this->function = this->analysis.entry;
this->ready = true; this->ready = true;
///////////////////////////////////////////////////////////////////////////////
////////////////// REMOVE BELOW //////////////////////////////////////
////////////////////////////////////////////////////////////
/* /*
bool showGraphRva = ConfigBool("Gui", "ShowGraphRva"); bool showGraphRva = ConfigBool("Gui", "ShowGraphRva");
Analysis anal; Analysis anal;
@ -1907,14 +1918,14 @@ void DisassemblerGraphView::followDisassemblerSlot()
void DisassemblerGraphView::colorsUpdatedSlot() void DisassemblerGraphView::colorsUpdatedSlot()
{ {
disassemblyBackgroundColor = Qt::white; disassemblyBackgroundColor = ConfigColor("gui.background");
graphNodeColor = Qt::black; graphNodeColor = ConfigColor("gui.border");
backgroundColor = QColor(220, 240, 255); backgroundColor = ConfigColor("gui.alt_background");
disassemblySelectionColor = Qt::yellow; disassemblySelectionColor = ConfigColor("gui.highlight");
jmpColor = Qt::blue; jmpColor = ConfigColor("graph.trufae");
brtrueColor = Qt::green; brtrueColor = ConfigColor("graph.true");
brfalseColor = Qt::red; brfalseColor = ConfigColor("graph.false");
/*disassemblyBackgroundColor = ConfigColor("GraphNodeBackgroundColor"); /*disassemblyBackgroundColor = ConfigColor("GraphNodeBackgroundColor");
if(!disassemblyBackgroundColor.alpha()) if(!disassemblyBackgroundColor.alpha())
disassemblyBackgroundColor = ConfigColor("DisassemblyBackgroundColor"); disassemblyBackgroundColor = ConfigColor("DisassemblyBackgroundColor");