From a65bcffffd8bd0647bac86d08d73e3311987deb0 Mon Sep 17 00:00:00 2001 From: wh201906 Date: Wed, 29 Apr 2020 17:12:35 +0800 Subject: [PATCH] Refactor file reading function --- module/mifare.cpp | 60 +++++++++++++++++++++++++++-------------------- module/mifare.h | 1 + ui/mainwindow.cpp | 2 +- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/module/mifare.cpp b/module/mifare.cpp index 68edee3..5bd6ddd 100644 --- a/module/mifare.cpp +++ b/module/mifare.cpp @@ -603,7 +603,7 @@ bool Mifare::data_loadDataFile(const QString& filename) if(file.open(QIODevice::ReadOnly)) { QByteArray buff; - buff = file.read(8192); + buff = file.read(10000); bool isBin = false; for(int i = 0; i < cardType.blocks * 16; i++) // Detect the file type { @@ -620,19 +620,11 @@ bool Mifare::data_loadDataFile(const QString& filename) } if(isBin) { - char LByte, RByte; - char map[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + if(file.size() < cardType.blocks * 16) + return false; for(int i = 0; i < cardType.blocks; i++) { - QString tmp = ""; - for(int j = 0; j < 16; j++) - { - LByte = map[(unsigned char)buff[i * 16 + j] >> 4]; - RByte = map[(unsigned char)buff[i * 16 + j] & 0xF]; - tmp += LByte; - tmp += RByte; - } - qDebug() << tmp; + QString tmp = bin2text(buff, i, 16); dataList->replace(i, tmp.toUpper()); } } @@ -662,23 +654,23 @@ bool Mifare::data_loadKeyFile(const QString& filename) if(file.open(QIODevice::ReadOnly)) { QByteArray buff; - buff = file.read(960); - bool isBin = true; - if(isBin) + buff = file.read(10000); + bool isKey = file.size() <= cardType.sectors * 14; + if(isKey) { - char LByte, RByte; - char map[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; for(int i = 0; i < cardType.sectors; i++) { - QString tmp = ""; - for(int j = 0; j < 12; j++) - { - LByte = map[(unsigned char)buff[i * 12 + j] >> 4]; - RByte = map[(unsigned char)buff[i * 12 + j] & 0xF]; - tmp += LByte; - tmp += RByte; - } - qDebug() << tmp; + QString tmp = bin2text(buff, i, 12); + keyAList->replace(i, tmp.left(12).toUpper()); + keyBList->replace(i, tmp.right(12).toUpper()); + } + } + else + { + for(int i = 0; i < cardType.sectors; i++) + { + int blk = cardType.blks[i] + cardType.blk[i] - 1; + QString tmp = bin2text(buff, blk, 16); keyAList->replace(i, tmp.left(12).toUpper()); keyBList->replace(i, tmp.right(12).toUpper()); } @@ -693,6 +685,22 @@ bool Mifare::data_loadKeyFile(const QString& filename) } } +QString Mifare::bin2text(const QByteArray& buff, int i, int length) +{ + QString ret = ""; + char LByte, RByte; + char map[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + for(int j = 0; j < length; j++) + { + LByte = map[(unsigned char)buff[i * length + j] >> 4]; + RByte = map[(unsigned char)buff[i * length + j] & 0xF]; + ret += LByte; + ret += RByte; + } + qDebug() << ret; + return ret; +} + bool Mifare::data_saveDataFile(const QString& filename, bool isBin) { QFile file(filename, this); diff --git a/module/mifare.h b/module/mifare.h index 0205a84..8f31ead 100644 --- a/module/mifare.h +++ b/module/mifare.h @@ -120,6 +120,7 @@ private: QRegExp* chkKeyPattern; QRegExp* nestedKeyPattern; QRegExp* UIDPattern; + QString bin2text(const QByteArray &buff, int start, int length); }; #endif // MIFARE_H diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index ab6aecb..3fdf3f2 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -274,7 +274,7 @@ void MainWindow::on_MF_File_loadButton_clicked() else if(ui->MF_File_keyBox->isChecked()) { title = tr("Plz select the key file:"); - filename = QFileDialog::getOpenFileName(this, title, "./", tr("Binary Key Files(*.bin *.dump);;All Files(*.*)")); + filename = QFileDialog::getOpenFileName(this, title, "./", tr("Binary Key Files(*.bin *.dump);;Binary Data Files(*.bin *.dump);;All Files(*.*)")); qDebug() << filename; if(filename != "") {