123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- //
- // Created by robin on 7/22/16.
- //
-
- #include "FreeFareTagBusiness.h"
-
- FreeFareTagBusiness::FreeFareTagBusiness(std::shared_ptr<FreeFareTag> tag)
- : _tag(tag)
- {
- }
-
- ResultBool FreeFareTagBusiness::authenticate(int sector, std::string key, int keyType)
- {
- return _tag->authenticate(sector, key, keyType);
- }
-
- ResultString FreeFareTagBusiness::readBlock(int sector, int block, std::string key, int keyType)
- {
- return _tag->readBlock(sector, block, key, keyType);
- }
-
- Result<SectorDbo> FreeFareTagBusiness::readSector(int sector, std::string key, int keyType)
- {
- std::string res;
- int lastBlock = _tag->getSectorBlockCount(sector);
- for (int i = 0; i < lastBlock; ++i) {
- auto data = readBlock(sector, i, key, keyType);
- if (data) {
- res += data.getData();
- }
- else {
- return Result<SectorDbo>::error(data);
- }
- }
- return Result<SectorDbo>::ok(SectorDbo(res));
- }
-
- const std::string &FreeFareTagBusiness::getUid() const
- {
- return _tag->getUid();
- }
-
- freefare_tag_type FreeFareTagBusiness::getType() const
- {
- return _tag->getType();
- }
-
- std::shared_ptr<FreeFareTag> FreeFareTagBusiness::getTag() const
- {
- return _tag;
- }
-
- Result<MappedKeys> FreeFareTagBusiness::mapKeys(std::vector<std::string> keys, std::function<void(int, int)> cb)
- {
- MappedKeys mappedKeys;
-
- for (int i = 0; i < 16; ++i) {
- std::pair<std::string, std::string> blockKeys;
- for (int k = 0; k < keys.size(); ++k) {
- auto key = keys[k];
- if (authenticate(i, key, MFC_KEY_A)) {
- blockKeys.first = key;
- }
- if (authenticate(i, key, MFC_KEY_B)) {
- blockKeys.second = key;
- }
- if (cb != 0) {
- cb((i * keys.size()) + k + 1, 16 * keys.size());
- }
- if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
- break;
- }
- }
- mappedKeys.push_back(blockKeys);
- }
- if (cb != 0) {
- cb(16 * keys.size(), 16 * keys.size());
- }
-
- return Result<MappedKeys>::ok(mappedKeys);
- }
-
- Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(MappedKeys keys, std::function<void(int, int)> cb)
- {
- if (keys.size() != 16) {
- return Result<std::vector<SectorDbo>>::error("Must have 16 sectors");
- }
- std::vector<SectorDbo> sectors;
- for (int s = 0; s < keys.size(); ++s) {
- auto sectorKey = keys[s];
- SectorDbo sector;
- bool keyA = false;
- bool keyB = false;
- for (int b = 0; b < 3; ++b) {
- std::string data = "";
- if (!sectorKey.first.empty()) {
- auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
- if (blockResult) {
- data = blockResult.getData();
- keyA = true;
- }
- }
- if (!sectorKey.second.empty()) {
- auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
- if (blockResult) {
- if (data.empty()) {
- data = blockResult.getData();
- }
- keyB = true;
- }
- }
- sector.setBlock(b, data);
- if (cb != 0) {
- cb((s * 4) + b + 1, keys.size() * 4);
- }
- }
- int b = 3;
- std::string dataA = "";
- std::string dataB = "";
- if (!sectorKey.first.empty()) {
- auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
- if (blockResult) {
- dataA = blockResult.getData();
- keyA = true;
- }
- }
- if (!sectorKey.second.empty()) {
- auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
- if (blockResult) {
- dataB = blockResult.getData();
- keyB = true;
- }
- }
- if (cb != 0) {
- cb((s * 4) + b + 1, keys.size() * 4);
- }
-
- sector.setBlock(b, dataA);
- AccessBitsDbo accessBitsDboA = sector.getAccessBitsDbo();
- sector.setBlock(b, dataB);
- AccessBitsDbo accessBitsDboB = sector.getAccessBitsDbo();
- sector.setKeyA(keyA ? sectorKey.first : "");
- sector.setKeyB(keyB ? sectorKey.second : "");
-
- std::string accessBits;
- if (keyA && accessBitsDboA.canKeyAReadAccessBitsTrailer()) {
- accessBits = accessBitsDboA.getBits();
- }
- else if (keyB && accessBitsDboB.canKeyBReadAccessBitsTrailer()) {
- accessBits = accessBitsDboB.getBits();
- }
- sector.setAccessBits(accessBits);
-
- sectors.push_back(sector);
- }
-
- return Result<std::vector<SectorDbo>>::ok(sectors);
- }
-
- Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(std::vector<std::string> keys, std::function<void(int, int)> mapCb, std::function<void(int, int)> dumpCb)
- {
- auto mappedKeysResult = mapKeys(keys, mapCb);
- if (!mappedKeysResult) {
- return Result<std::vector<SectorDbo>>::error(mappedKeysResult);
- }
- return dump(mappedKeysResult.getData(), dumpCb);
- }
|