summaryrefslogtreecommitdiff
path: root/examples/triage/strings.cpp
blob: f7fe5989acf76ec2085a5f8cfedad2415d993a3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <string.h>
#include <algorithm>
#include "strings.h"
#include "view.h"
#include "fontsettings.h"


GenericStringsModel::GenericStringsModel(QWidget* parent, BinaryViewRef data) : QAbstractItemModel(parent)
{
	m_data = data;
	m_totalCols = 3;
	m_sortCol = 0;
	m_sortOrder = Qt::AscendingOrder;
	m_allEntries = data->GetStrings();
	m_entries = m_allEntries;
}


int GenericStringsModel::columnCount(const QModelIndex&) const
{
	return m_totalCols;
}


int GenericStringsModel::rowCount(const QModelIndex& parent) const
{
	if (parent.isValid())
		return 0;
	return (int)m_entries.size();
}


QVariant GenericStringsModel::data(const QModelIndex& index, int role) const
{
	switch (role)
	{
	case Qt::DisplayRole:
		if (!index.isValid() || index.row() >= (int)m_entries.size())
			return QVariant();
		if (index.column() == 0)
			return QString("0x") + QString::number(m_entries[index.row()].start, 16);
		if (index.column() == 1)
			return QString::number(m_entries[index.row()].length);
		if (index.column() == 2)
			return stringRefToQString(m_entries[index.row()]).replace("\n", "\\n");
		break;
	case Qt::ForegroundRole:
		if (index.column() == 0)
			return getThemeColor(AddressColor);
		break;
	}

	return QVariant();
}


QVariant GenericStringsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (orientation == Qt::Vertical)
		return QVariant();
	if (role != Qt::DisplayRole)
		return QVariant();
	if (section == 0)
		return QString("Address");
	if (section == 1)
		return QString("Length");
	if (section == 2)
		return QString("String");
	return QVariant();
}


QModelIndex GenericStringsModel::index(int row, int col, const QModelIndex& parent) const
{
	if (parent.isValid())
		return QModelIndex();
	if (row >= (int)m_entries.size())
		return QModelIndex();
	if (col >= m_totalCols)
		return QModelIndex();
	return createIndex(row, col);
}


QModelIndex GenericStringsModel::parent(const QModelIndex&) const
{
	return QModelIndex();
}


QString GenericStringsModel::stringRefToQString(const BNStringReference& stringRef) const
{
	QString qstr;
	BinaryNinja::DataBuffer stringBuffer = m_data->ReadBuffer(stringRef.start, stringRef.length);

	if (stringRef.type == BNStringType::Utf32String)
	{	
		char32_t* data = (char32_t*)stringBuffer.GetData();
		qstr = QString::fromUcs4(data, stringRef.length / 4);
	} 
	else if (stringRef.type == BNStringType::Utf16String)
	{
		char16_t* data = (char16_t*)stringBuffer.GetData();
		qstr = QString::fromUtf16(data, stringRef.length / 2);
	}
	else 
	{
		char* data = (char*)stringBuffer.GetData();
		qstr = QString::fromUtf8(data, stringBuffer.GetLength());
	}

	return qstr;
}


BNStringReference GenericStringsModel::getStringRefAt(const QModelIndex& index) const
{
	if (!index.isValid() || index.row() >= (int)m_entries.size())
		return BNStringReference{};
	return m_entries[index.row()];
}

void GenericStringsModel::performSort(int col, Qt::SortOrder order)
{
	std::sort(m_entries.begin(), m_entries.end(), [&](BNStringReference a, BNStringReference b) {
		if (col == 0)
		{
			if (order == Qt::AscendingOrder)
				return a.start < b.start;
			else
				return a.start > b.start;
		}
		else if (col == 1)
		{
			if (order == Qt::AscendingOrder)
				return a.length < b.length;
			else
				return a.length > b.length;
		}
		else if (col == 2)
		{	
			QString s = stringRefToQString(a);
			QString s2 = stringRefToQString(b);

			if (order == Qt::AscendingOrder)
				return s < s2;
			else
				return s > s2;
		}
		return false;
	});
}


void GenericStringsModel::sort(int col, Qt::SortOrder order)
{
	beginResetModel();
	m_sortCol = col;
	m_sortOrder = order;
	performSort(col, order);
	endResetModel();
}


void GenericStringsModel::setFilter(const std::string& filterText)
{
	beginResetModel();
	m_entries.clear();
	for (auto& entry : m_allEntries)
	{
		auto s = stringRefToQString(entry).toStdString();
		
		if (FilteredView::match(s, filterText))
			m_entries.push_back(entry);
	}
	performSort(m_sortCol, m_sortOrder);
	endResetModel();
}


StringsTreeView::StringsTreeView(StringsWidget* parent, TriageView* view, BinaryViewRef data) : QTreeView(parent)
{
	m_data = data;
	m_parent = parent;
	m_view = view;

	// Allow view-specific shortcuts when strings are focused
	m_actionHandler.setupActionHandler(this);
	m_actionHandler.setActionContext([=, this]() { return m_view->actionContext(); });

	m_model = new GenericStringsModel(this, m_data);
	setModel(m_model);
	setRootIsDecorated(false);
	setUniformRowHeights(true);
	setSortingEnabled(true);
	sortByColumn(0, Qt::AscendingOrder);

	setFont(getMonospaceFont(this));

	connect(selectionModel(), &QItemSelectionModel::currentChanged, this, &StringsTreeView::stringSelected);
	connect(this, &QTreeView::doubleClicked, this, &StringsTreeView::stringDoubleClicked);
}


void StringsTreeView::stringSelected(const QModelIndex& cur, const QModelIndex&)
{
	BNStringReference stringRef = m_model->getStringRefAt(cur);
	if (stringRef.start == 0)
		return;

	m_view->setCurrentOffset(stringRef.start);
}


void StringsTreeView::stringDoubleClicked(const QModelIndex& cur)
{
	BNStringReference stringRef = m_model->getStringRefAt(cur);
	if (stringRef.start == 0)
		return;

	ViewFrame* viewFrame = ViewFrame::viewFrameForWidget(this);
	if (viewFrame)
	{
		viewFrame->navigate("Linear:" + viewFrame->getCurrentDataType(),  stringRef.start);
	}
}


void StringsTreeView::setFilter(const std::string& filterText)
{
	m_model->setFilter(filterText);
}


void StringsTreeView::scrollToFirstItem()
{
	scrollToTop();
}


void StringsTreeView::scrollToCurrentItem()
{
	scrollTo(currentIndex());
}


void StringsTreeView::selectFirstItem()
{
	setCurrentIndex(m_model->index(0, 0, QModelIndex()));
}


void StringsTreeView::activateFirstItem()
{
	stringDoubleClicked(m_model->index(0, 0, QModelIndex()));
}


void StringsTreeView::closeFilter()
{
	setFocus(Qt::OtherFocusReason);
}


void StringsTreeView::keyPressEvent(QKeyEvent* event)
{
	if ((event->text().size() == 1) && (event->text()[0] > ' ') && (event->text()[0] <= '~'))
	{
		m_parent->showFilter(event->text());
		event->accept();
	}
	else if ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter))
	{
		QList<QModelIndex> sel = selectionModel()->selectedIndexes();
		if (sel.size() != 0)
			stringDoubleClicked(sel[0]);
	}
	QTreeView::keyPressEvent(event);
}


StringsWidget::StringsWidget(QWidget* parent, TriageView* view, BinaryViewRef data) : QWidget(parent)
{
	QVBoxLayout* layout = new QVBoxLayout();
	layout->setContentsMargins(0, 0, 0, 0);
	StringsTreeView* strings = new StringsTreeView(this, view, data);
	m_filter = new FilteredView(this, strings, strings);
	m_filter->setFilterPlaceholderText("Search strings");
	layout->addWidget(m_filter, 1);
	setLayout(layout);
	setMinimumSize(UIContext::getScaledWindowSize(100, 196));
}


void StringsWidget::showFilter(const QString& filter)
{
	m_filter->showFilter(filter);
}