<commit_before>#include <string.h>
#include <time.h>
#include "headers.h"
#include "fontsettings.h"
#include "theme.h"
#include "viewframe.h"


NavigationLabel::NavigationLabel(const QString& text, QColor color, const std::function<void()>& func):
	QLabel(text), m_func(func)
{
	QPalette style(palette());
	style.setColor(QPalette::WindowText, color);
	setPalette(style);
	setFont(getMonospaceFont(this));
}


void NavigationLabel::mousePressEvent(QMouseEvent*)
{
	m_func();
}


NavigationAddressLabel::NavigationAddressLabel(const QString& text):
	NavigationLabel(text, getThemeColor(AddressColor), [this]() { clickEvent(); })
{
	m_address = text.toULongLong(nullptr, 0);
}


void NavigationAddressLabel::clickEvent()
{
	ViewFrame* viewFrame = ViewFrame::viewFrameForWidget(this);
	if (viewFrame)
		viewFrame->navigate("Linear:" + viewFrame->getCurrentDataType(), m_address);
}


NavigationCodeLabel::NavigationCodeLabel(const QString& text):
	NavigationLabel(text, getThemeColor(CodeSymbolColor), [this]() { clickEvent(); })
{
	m_address = text.toULongLong(nullptr, 0);
}


void NavigationCodeLabel::clickEvent()
{
	ViewFrame* viewFrame = ViewFrame::viewFrameForWidget(this);
	if (viewFrame)
		viewFrame->navigate("Graph:" + viewFrame->getCurrentDataType(), m_address);
}


Headers::Headers(): m_columns(1), m_rowsPerColumn(8)
{
}


void Headers::AddField(const QString& title, const QString& value, HeaderFieldType type)
{
	m_fields.push_back(HeaderField { title, {value}, type });
}


void Headers::AddField(const QString& title, const std::vector<QString>& values, HeaderFieldType type)
{
	m_fields.push_back(HeaderField { title, values, type });
}


GenericHeaders::GenericHeaders(BinaryViewRef data)
{
	AddField("Type", QString::fromStdString(data->GetTypeName()));
	if (data->GetDefaultPlatform())
		AddField("Platform", QString::fromStdString(data->GetDefaultPlatform()->GetName()));
	if (data->IsValidOffset(data->GetEntryPoint()))
		AddField("Entry Point", QString("0x") + QString::number(data->GetEntryPoint(), 16), CodeHeaderField);
	if (data->IsValidOffset(data->GetStart()))
		AddField("Current Base", QString("0x") + QString::number(data->GetStart(), 16), AddressHeaderField);
}


PEHeaders::PEHeaders(BinaryViewRef data)
{
	uint64_t peOffset = data->GetStart() + GetValueOfStructMember(data, "DOS_Header", data->GetStart(), "e_lfanew");
	uint64_t optHeaderStart = GetAddressAfterStruct(data, "COFF_Header", peOffset);

	BinaryNinja::DataBuffer peMagic = data->ReadBuffer(optHeaderStart, 2);
	bool is64bit;
	std::string optHeaderName;
	if ((peMagic.GetLength() == 2) && (peMagic[0] == 0x0b) && (peMagic[1] == 0x01))
	{
		optHeaderName = "PE32_Optional_Header";
		AddField("Type", "PE 32-bit");
		is64bit = false;
	}
	else if ((peMagic.GetLength() == 2) && (peMagic[0] == 0x0b) && (peMagic[1] == 0x02))
	{
		optHeaderName = "PE64_Optional_Header";
		AddField("Type", "PE 64-bit");
		is64bit = true;
	}
	else
	{
		AddField("Type", QString::fromStdString(data->GetTypeName()));
		if (data->GetDefaultPlatform())
			AddField("Platform", QString::fromStdString(data->GetDefaultPlatform()->GetName()));
		if (data->IsValidOffset(data->GetEntryPoint()))
			AddField("Entry Point", QString("0x") + QString::number(data->GetEntryPoint(), 16), CodeHeaderField);
		return;
	}

	uint64_t machineValue = GetValueOfStructMember(data, "COFF_Header", peOffset, "machine");
	QString machineName = GetNameOfEnumerationMember(data, "coff_machine", machineValue);
	if (machineName.startsWith("IMAGE_FILE_MACHINE_"))
		machineName = machineName.mid((int)strlen("IMAGE_FILE_MACHINE_"));
	AddField("Machine", machineName);

	uint64_t subsysValue = GetValueOfStructMember(data, optHeaderName, optHeaderStart, "subsystem");
	QString subsysName = GetNameOfEnumerationMember(data, "pe_subsystem", subsysValue);
	if (subsysName.startsWith("IMAGE_SUBSYSTEM_"))
		subsysName = subsysName.mid((int)strlen("IMAGE_SUBSYSTEM_"));
	AddField("Subsystem", subsysName);

	uint64_t secs = GetValueOfStructMember(data, "COFF_Header", peOffset, "timeDateStamp");
	QDateTime t = QDateTime::fromSecsSinceEpoch(secs);
	AddField("Timestamp", t.toString());

	AddField("Current Base", QString("0x") + QString::number(data->GetStart(), 16), AddressHeaderField);

	uint64_t base = GetValueOfStructMember(data, optHeaderName, optHeaderStart, "imageBase");
	AddField("Image Base", QString("0x") + QString::number(base, 16), AddressHeaderField);

	uint64_t entryPoint = base + GetValueOfStructMember(data, optHeaderName, optHeaderStart, "addressOfEntryPoint");
	AddField("Entry Point", QString("0x") + QString::number(entryPoint, 16), CodeHeaderField);

	uint64_t sectionAlign = GetValueOfStructMember(data, optHeaderName, optHeaderStart, "sectionAlignment");
	AddField("Section Alignment", QString("0x") + QString::number(sectionAlign, 16));

	uint64_t fileAlign = GetValueOfStructMember(data, optHeaderName, optHeaderStart, "fileAlignment");<commit_msg>Show endianness information in the triage view<commit_after>#include <string.h>
#include <time.h>
#include "headers.h"
#include "fontsettings.h"
#include "theme.h"
#include "viewframe.h"


NavigationLabel::NavigationLabel(const QString& text, QColor color, const std::function<void()>& func):
	QLabel(text), m_func(func)
{
	QPalette style(palette());
	style.setColor(QPalette::WindowText, color);
	setPalette(style);
	setFont(getMonospaceFont(this));
}


void NavigationLabel::mousePressEvent(QMouseEvent*)
{
	m_func();
}


NavigationAddressLabel::NavigationAddressLabel(const QString& text):
	NavigationLabel(text, getThemeColor(AddressColor), [this]() { clickEvent(); })
{
	m_address = text.toULongLong(nullptr, 0);
}


void NavigationAddressLabel::clickEvent()
{
	ViewFrame* viewFrame = ViewFrame::viewFrameForWidget(this);
	if (viewFrame)
		viewFrame->navigate("Linear:" + viewFrame->getCurrentDataType(), m_address);
}


NavigationCodeLabel::NavigationCodeLabel(const QString& text):
	NavigationLabel(text, getThemeColor(CodeSymbolColor), [this]() { clickEvent(); })
{
	m_address = text.toULongLong(nullptr, 0);
}


void NavigationCodeLabel::clickEvent()
{
	ViewFrame* viewFrame = ViewFrame::viewFrameForWidget(this);
	if (viewFrame)
		viewFrame->navigate("Graph:" + viewFrame->getCurrentDataType(), m_address);
}


Headers::Headers(): m_columns(1), m_rowsPerColumn(8)
{
}


void Headers::AddField(const QString& title, const QString& value, HeaderFieldType type)
{
	m_fields.push_back(HeaderField { title, {value}, type });
}


void Headers::AddField(const QString& title, const std::vector<QString>& values, HeaderFieldType type)
{
	m_fields.push_back(HeaderField { title, values, type });
}


GenericHeaders::GenericHeaders(BinaryViewRef data)
{
	AddField("Type", QString::fromStdString(data->GetTypeName()));
	if (data->GetDefaultPlatform())
		AddField("Platform", QString::fromStdString(data->GetDefaultPlatform()->GetName()));
	if (data->IsValidOffset(data->GetEntryPoint()))
		AddField("Entry Point", QString("0x") + QString::number(data->GetEntryPoint(), 16), CodeHeaderField);
	if (data->IsValidOffset(data->GetStart()))
		AddField("Current Base", QString("0x") + QString::number(data->GetStart(), 16), AddressHeaderField);
	AddField("Endianness", data->GetDefaultEndianness() == BigEndian ? "Big" : "Little");
}


PEHeaders::PEHeaders(BinaryViewRef data)
{
	uint64_t peOffset = data->GetStart() + GetValueOfStructMember(data, "DOS_Header", data->GetStart(), "e_lfanew");
	uint64_t optHeaderStart = GetAddressAfterStruct(data, "COFF_Header", peOffset);

	BinaryNinja::DataBuffer peMagic = data->ReadBuffer(optHeaderStart, 2);
	bool is64bit;
	std::string optHeaderName;
	if ((peMagic.GetLength() == 2) && (peMagic[0] == 0x0b) && (peMagic[1] == 0x01))
	{
		optHeaderName = "PE32_Optional_Header";
		AddField("Type", "PE 32-bit");
		is64bit = false;
	}
	else if ((peMagic.GetLength() == 2) && (peMagic[0] == 0x0b) && (peMagic[1] == 0x02))
	{
		optHeaderName = "PE64_Optional_Header";
		AddField("Type", "PE 64-bit");
		is64bit = true;
	}
	else
	{
		AddField("Type", QString::fromStdString(data->GetTypeName()));
		if (data->GetDefaultPlatform())
			AddField("Platform", QString::fromStdString(data->GetDefaultPlatform()->GetName()));
		if (data->IsValidOffset(data->GetEntryPoint()))
			AddField("Entry Point", QString("0x") + QString::number(data->GetEntryPoint(), 16), CodeHeaderField);
		return;
	}

	uint64_t machineValue = GetValueOfStructMember(data, "COFF_Header", peOffset, "machine");
	QString machineName = GetNameOfEnumerationMember(data, "coff_machine", machineValue);
	if (machineName.startsWith("IMAGE_FILE_MACHINE_"))
		machineName = machineName.mid((int)strlen("IMAGE_FILE_MACHINE_"));
	AddField("Machine", machineName);

	AddField("Endianness", data->GetDefaultEndianness() == BigEndian ? "Big" : "Little");

	uint64_t subsysValue = GetValueOfStructMember(data, optHeaderName, optHeaderStart, "subsystem");
	QString subsysName = GetNameOfEnumerationMember(data, "pe_subsystem", subsysValue);
	if (subsysName.startsWith("IMAGE_SUBSYSTEM_"))
		subsysName = subsysName.mid((int)strlen("IMAGE_SUBSYSTEM_"));
	AddField("Subsystem", subsysName);

	uint64_t secs = GetValueOfStructMember(data, "COFF_Header", peOffset, "timeDateStamp");
	QDateTime t = QDateTime::fromSecsSinceEpoch(secs);
	AddField("Timestamp", t.toString());

	AddField("Current Base", QString("0x") + QString::number(data->GetStart(), 16), AddressHeaderField);

	uint64_t base = GetValueOfStructMember(data, optHeaderName, optHeaderStart, "imageBase");
	AddField("Image Base", QString("0x") + QString::number(base, 16), AddressHeaderField);

	uint64_t entryPoint = base + GetValueOfStructMember(data, optHeaderName, optHeaderStart, "addressOfEntryPoint");
	AddField("Entry Point", QString("0x") + QString::number(entryPoint, 16), CodeHeaderField);

	uint64_t sectionAlign = GetValueOfStructMember(data, optHeaderName, optHeaderStart, "sectionAlignment");
	AddField("Section Alignment", QString("0x") + QString::number(sectionAlign, 16));

	uint64_t fileAlign = GetValueOfStructMember(data, optHeaderName, optHeaderStart, "fileAlignment");
{'fraction_non_alphanumeric': 0.1578423566878981, 'fraction_numerical': 0.014729299363057325, 'mean_word_length': 8.219266055045871, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6315160', 'n_tokens_mistral': 3427, 'n_tokens_neox': 3259, 'n_words': 583}