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)) if(file.open(QIODevice::ReadOnly))
{ {
QByteArray buff; QByteArray buff;
buff = file.read(8192); buff = file.read(10000);
bool isBin = false; bool isBin = false;
for(int i = 0; i < cardType.blocks * 16; i++) // Detect the file type 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) if(isBin)
{ {
char LByte, RByte; if(file.size() < cardType.blocks * 16)
char map[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; return false;
for(int i = 0; i < cardType.blocks; i++) for(int i = 0; i < cardType.blocks; i++)
{ {
QString tmp = ""; QString tmp = bin2text(buff, i, 16);
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;
dataList->replace(i, tmp.toUpper()); dataList->replace(i, tmp.toUpper());
} }
} }
@ -662,23 +654,23 @@ bool Mifare::data_loadKeyFile(const QString& filename)
if(file.open(QIODevice::ReadOnly)) if(file.open(QIODevice::ReadOnly))
{ {
QByteArray buff; QByteArray buff;
buff = file.read(960); buff = file.read(10000);
bool isBin = true; bool isKey = file.size() <= cardType.sectors * 14;
if(isBin) 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++) for(int i = 0; i < cardType.sectors; i++)
{ {
QString tmp = ""; QString tmp = bin2text(buff, i, 12);
for(int j = 0; j < 12; j++) keyAList->replace(i, tmp.left(12).toUpper());
{ keyBList->replace(i, tmp.right(12).toUpper());
LByte = map[(unsigned char)buff[i * 12 + j] >> 4];
RByte = map[(unsigned char)buff[i * 12 + j] & 0xF];
tmp += LByte;
tmp += RByte;
} }
qDebug() << tmp; }
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()); keyAList->replace(i, tmp.left(12).toUpper());
keyBList->replace(i, tmp.right(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) bool Mifare::data_saveDataFile(const QString& filename, bool isBin)
{ {
QFile file(filename, this); QFile file(filename, this);

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

@ -274,7 +274,7 @@ void MainWindow::on_MF_File_loadButton_clicked()
else if(ui->MF_File_keyBox->isChecked()) else if(ui->MF_File_keyBox->isChecked())
{ {
title = tr("Plz select the key file:"); 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; qDebug() << filename;
if(filename != "") if(filename != "")
{ {

Loading…
Cancel
Save