Proxmark3GUI/ui/mf_trailerdecoderdialog.cpp

155 lines
5.2 KiB
C++
Raw Normal View History

2020-08-04 23:33:14 +08:00
#include "mf_trailerdecoderdialog.h"
#include "ui_mf_trailerdecoderdialog.h"
MF_trailerDecoderDialog::MF_trailerDecoderDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::MF_trailerDecoderDialog)
{
ui->setupUi(this);
QRegularExpression re("(([0-9a-fA-F]{2} ){0,4})|([0-9a-fA-F]{0,8})");
validator = new QRegularExpressionValidator(this);
validator->setRegularExpression(re);
ui->accessBitsEdit->setValidator(validator);
sizeGroup = new QButtonGroup(this);
sizeGroup->addButton(ui->size4Button, 4);
sizeGroup->addButton(ui->size16Button, 16);
connect(sizeGroup, QOverload<int, bool>::of(&QButtonGroup::buttonToggled), this, &MF_trailerDecoderDialog::on_blockSizeChanged);
connect(ui->C0Box, &QSpinBox::textChanged, this, &MF_trailerDecoderDialog::on_boxChanged);
connect(ui->C1Box, &QSpinBox::textChanged, this, &MF_trailerDecoderDialog::on_boxChanged);
connect(ui->C2Box, &QSpinBox::textChanged, this, &MF_trailerDecoderDialog::on_boxChanged);
connect(ui->C3Box, &QSpinBox::textChanged, this, &MF_trailerDecoderDialog::on_boxChanged);
2020-08-04 23:33:14 +08:00
ui->dataBlockWidget->setRowCount(3);
ui->dataBlockWidget->setColumnCount(4);
ui->trailerBlockWidget->setRowCount(2);
ui->trailerBlockWidget->setColumnCount(3);
}
MF_trailerDecoderDialog::~MF_trailerDecoderDialog()
{
delete ui;
}
void MF_trailerDecoderDialog::on_accessBitsEdit_textChanged(const QString &arg1)
2020-08-04 23:33:14 +08:00
{
ui->C0Box->blockSignals(true);
ui->C1Box->blockSignals(true);
ui->C2Box->blockSignals(true);
ui->C3Box->blockSignals(true);
QList<quint8> ACBits = Mifare::data_getACBits(arg1);
if(ACBits.size() == 0)
2020-08-04 23:33:14 +08:00
{
ui->isAccessBitsValidLabel->setStyleSheet("color:rgb(200, 0, 0)");
ui->isAccessBitsValidLabel->setText(tr("Invalid!\nIt could make the whole sector blocked irreversibly!"));
2020-08-04 23:33:14 +08:00
}
else
{
ui->C0Box->setValue(ACBits[0]);
ui->C1Box->setValue(ACBits[1]);
ui->C2Box->setValue(ACBits[2]);
ui->C3Box->setValue(ACBits[3]);
2020-08-04 23:33:14 +08:00
ui->isAccessBitsValidLabel->setStyleSheet("color:rgb(0, 200, 0)");
ui->isAccessBitsValidLabel->setText(tr("Valid"));
bool isKeyBReadable = ACBits[3] == 0 || ACBits[3] == 1 || ACBits[3] == 4;
2020-08-05 00:45:54 +08:00
for(int j = 0; j < 3; j++)
{
2020-08-06 11:19:56 +08:00
setTableItem(ui->trailerBlockWidget, 0, j, Mifare::trailerReadCondition[ACBits[3]][j]);
setTableItem(ui->trailerBlockWidget, 1, j, Mifare::trailerWriteCondition[ACBits[3]][j]);
2020-08-05 00:45:54 +08:00
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
2020-08-06 11:19:56 +08:00
Mifare::AccessType type = Mifare::dataCondition[ACBits[i]][j];
if(type == Mifare::ACC_KEY_B && isKeyBReadable)
2020-08-05 00:45:54 +08:00
{
2020-08-06 11:19:56 +08:00
type = Mifare::ACC_NEVER;
2020-08-05 00:45:54 +08:00
}
2020-08-06 11:19:56 +08:00
else if(type == Mifare::ACC_KEY_AB && isKeyBReadable)
2020-08-05 00:45:54 +08:00
{
2020-08-06 11:19:56 +08:00
type = Mifare::ACC_KEY_A;
2020-08-05 00:45:54 +08:00
}
setTableItem(ui->dataBlockWidget, i, j, type);
}
}
2020-08-04 23:33:14 +08:00
}
ui->C0Box->blockSignals(false);
ui->C1Box->blockSignals(false);
ui->C2Box->blockSignals(false);
ui->C3Box->blockSignals(false);
2020-08-04 23:33:14 +08:00
}
void MF_trailerDecoderDialog::on_blockSizeChanged(int id, bool st)
{
if(st)
{
if(id == 4)
{
ui->dataBlockWidget->verticalHeaderItem(0)->setText("Block0");
ui->dataBlockWidget->verticalHeaderItem(1)->setText("Block1");
ui->dataBlockWidget->verticalHeaderItem(2)->setText("Block2");
}
else if(id == 16)
{
ui->dataBlockWidget->verticalHeaderItem(0)->setText("Block0~4");
ui->dataBlockWidget->verticalHeaderItem(1)->setText("Block5~9");
ui->dataBlockWidget->verticalHeaderItem(2)->setText("Block10~14");
}
}
}
2020-08-06 11:19:56 +08:00
void MF_trailerDecoderDialog::setTableItem(QTableWidget* widget, int row, int column, Mifare::AccessType accessType)
2020-08-04 23:33:14 +08:00
{
if(widget->item(row, column) == nullptr)
widget->setItem(row, column, new QTableWidgetItem());
QString text;
2020-08-06 11:19:56 +08:00
if(accessType == Mifare::ACC_NEVER)
2020-08-04 23:33:14 +08:00
{
text = "X";
}
2020-08-06 11:19:56 +08:00
else if(accessType == Mifare::ACC_KEY_A)
2020-08-04 23:33:14 +08:00
{
text = "KeyA";
}
2020-08-06 11:19:56 +08:00
else if(accessType == Mifare::ACC_KEY_B)
2020-08-04 23:33:14 +08:00
{
text = "KeyB";
}
2020-08-06 11:19:56 +08:00
else if(accessType == Mifare::ACC_KEY_AB)
2020-08-04 23:33:14 +08:00
{
text = "KeyA+B";
}
widget->item(row, column)->setText(text);
}
void MF_trailerDecoderDialog::on_boxChanged(const QString &arg1)
{
quint8 ACBits[4];
ACBits[0] = ui->C0Box->value();
ACBits[1] = ui->C1Box->value();
ACBits[2] = ui->C2Box->value();
ACBits[3] = ui->C3Box->value();
quint8 halfBytes[6] = {0, 0, 0, 0, 0, 0};
for(int i = 0; i < 4; i++)
{
halfBytes[2] |= (((ACBits[i] >> 0) & 1) << i);
halfBytes[5] |= (((ACBits[i] >> 1) & 1) << i);
halfBytes[4] |= (((ACBits[i] >> 2) & 1) << i);
}
halfBytes[0] = (~halfBytes[5]) & 0xf;
halfBytes[1] = (~halfBytes[2]) & 0xf;
halfBytes[3] = (~halfBytes[4]) & 0xf;
QString result;
for(int i = 0; i < 3; i++)
{
result += QString::number(halfBytes[i * 2], 16);
result += QString::number(halfBytes[i * 2 + 1], 16);
result += " ";
}
result = result.toUpper();
ui->accessBitsEdit->setText(result);
}