Refactor file reading function

pull/2/head
wh201906 5 years ago
parent 2b8e31e452
commit a65bcffffd

@ -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);

@ -120,6 +120,7 @@ private:
QRegExp* chkKeyPattern;
QRegExp* nestedKeyPattern;
QRegExp* UIDPattern;
QString bin2text(const QByteArray &buff, int start, int length);
};
#endif // MIFARE_H

@ -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 != "")
{

Loading…
Cancel
Save