diff --git a/common/util.cpp b/common/util.cpp index f7f7997..5eb3af9 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -1,10 +1,11 @@ #include "util.h" -Util::Util(QObject *parent) : QObject(parent) +Util::Util(Util::ClientType clientType, QObject *parent) : QObject(parent) { isRequiringOutput = false; requiredOutput = new QString(); timeStamp = QTime::currentTime(); + this->clientType = clientType; } void Util::processOutput(QString output) @@ -31,7 +32,7 @@ QString Util::execCMDWithOutput(QString cmd, unsigned long timeout) isRequiringOutput = true; requiredOutput->clear(); execCMD(cmd); - while( QTime::currentTime() < targetTime) + while(QTime::currentTime() < targetTime) { QApplication::processEvents(); if(timeStamp > currTime) @@ -47,6 +48,10 @@ QString Util::execCMDWithOutput(QString cmd, unsigned long timeout) void Util::delay(unsigned int msec) { QTime timer = QTime::currentTime().addMSecs(msec); - while( QTime::currentTime() < timer ) + while(QTime::currentTime() < timer) QApplication::processEvents(QEventLoop::AllEvents, 100); } +Util::ClientType Util::getClientType() +{ + return this->clientType; +} diff --git a/common/util.h b/common/util.h index 3b29a69..34c47fb 100644 --- a/common/util.h +++ b/common/util.h @@ -13,11 +13,17 @@ class Util : public QObject { Q_OBJECT public: - explicit Util(QObject *parent = nullptr); + enum ClientType + { + OFFICIAL, + ICEMAN, + }; + explicit Util(Util::ClientType clientType, QObject *parent = nullptr); void execCMD(QString cmd); QString execCMDWithOutput(QString cmd, unsigned long timeout = 2000); void delay(unsigned int msec); + ClientType getClientType(); public slots: void processOutput(QString output); @@ -28,6 +34,7 @@ private: signals: void refreshOutput(const QString& output); void write(QString data); + ClientType clientType; }; #endif // UTIL_H diff --git a/module/mifare.cpp b/module/mifare.cpp index fccc46f..a6960ee 100644 --- a/module/mifare.cpp +++ b/module/mifare.cpp @@ -114,6 +114,72 @@ void Mifare::list() ui->funcTab->setCurrentIndex(1); } +QString Mifare::_readblk(int blockId, KeyType keyType, QString& key, int waitTime) +{ + QString data; + QString result; + if(util->getClientType() == Util::OFFICIAL) + { + result = util->execCMDWithOutput( + "hf mf rdbl " + + QString::number(blockId) + + " " + + (char)keyType + + " " + + key, + waitTime); + if(result.indexOf("isOk:01") != -1) + { + result = result.mid(dataPattern->indexIn(result), 47).toUpper(); + if((blockId < 128 && ((blockId + 1) % 4 == 0)) || ((blockId + 1) % 8 == 0)) // process key block + { + if(keyType == KEY_A) + { + for(int i = 0; i < 6; i++) + { + result = result.replace(i * 3, 2, key.mid(i * 2, 2)); + } + data = result; + QString tmpKey = result.right(18).replace(" ", ""); + result = util->execCMDWithOutput( + "hf mf rdbl " + + QString::number(blockId) + + " B " + + tmpKey, + waitTime); + if(result.indexOf("isOk:01") == -1) + { + result = data; + result = result.replace(30, 17, "?? ?? ?? ?? ?? ??"); + data = result; + } + } + else + { + for(int i = 0; i < 6; i++) + { + result = result.replace( + 30 + i * 3, + 2, + key.mid(i * 2, 2)); + } + result = result.replace(0, 18, "?? ?? ?? ?? ?? ?? "); + data = result; + } + } + else + { + data = result; + } + } + else + { + data = ""; + } + return data; + } +} + void Mifare::read() { int waitTime = 300; diff --git a/module/mifare.h b/module/mifare.h index bb54d83..c496b1e 100644 --- a/module/mifare.h +++ b/module/mifare.h @@ -32,8 +32,8 @@ public: enum KeyType { - KEY_A, - KEY_B, + KEY_A = 'A', + KEY_B = 'B', }; enum DataType @@ -87,7 +87,7 @@ public: void data_clearData(); void data_clearKey(); - bool data_isKeyValid(const QString &key); + bool data_isKeyValid(const QString& key); Mifare::DataType data_isDataValid(QString data); void data_syncWithDataWidget(bool syncAll = true, int block = 0); void data_syncWithKeyWidget(bool syncAll = true, int sector = 0, KeyType keyType = KEY_A); @@ -102,15 +102,15 @@ public: void wipeC(); void setParameterC(); - bool data_loadDataFile(const QString &filename); - bool data_loadKeyFile(const QString &filename); + bool data_loadDataFile(const QString& filename); + bool data_loadKeyFile(const QString& filename); bool data_saveDataFile(const QString& filename, bool isBin); - bool data_saveKeyFile(const QString &filename, bool isBin); + bool data_saveKeyFile(const QString& filename, bool isBin); void data_key2Data(); void data_data2Key(); - void data_setData(int block, const QString &data); - void data_setKey(int sector, KeyType keyType, const QString &key); + void data_setData(int block, const QString& data); + void data_setKey(int sector, KeyType keyType, const QString& key); void lockC(); void writeAllE(); void readAllE(); @@ -133,7 +133,9 @@ private: QRegExp* chkKeyPattern; QRegExp* nestedKeyPattern; QRegExp* UIDPattern; - QString bin2text(const QByteArray &buff, int start, int length); + QString bin2text(const QByteArray& buff, int start, int length); + + QString _readblk(int blockId, KeyType keyType, QString &key, int waitTime); }; #endif // MIFARE_H