You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FreeFareTagBusiness.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Created by robin on 7/22/16.
  3. //
  4. #include "FreeFareTagBusiness.h"
  5. FreeFareTagBusiness::FreeFareTagBusiness(std::shared_ptr<FreeFareTag> tag)
  6. : _tag(tag)
  7. {
  8. }
  9. ResultBool FreeFareTagBusiness::authenticate(int sector, std::string key, int keyType)
  10. {
  11. return _tag->authenticate(sector, key, keyType);
  12. }
  13. ResultString FreeFareTagBusiness::readBlock(int sector, int block, std::string key, int keyType)
  14. {
  15. return _tag->readBlock(sector, block, key, keyType);
  16. }
  17. Result<SectorDbo> FreeFareTagBusiness::readSector(int sector, std::string key, int keyType)
  18. {
  19. std::string res;
  20. int lastBlock = _tag->getSectorBlockCount(sector);
  21. for (int i = 0; i < lastBlock; ++i) {
  22. auto data = readBlock(sector, i, key, keyType);
  23. if (data) {
  24. res += data.getData();
  25. }
  26. else {
  27. return Result<SectorDbo>::error(data);
  28. }
  29. }
  30. return Result<SectorDbo>::ok(SectorDbo(res));
  31. }
  32. const std::string &FreeFareTagBusiness::getUid() const
  33. {
  34. return _tag->getUid();
  35. }
  36. freefare_tag_type FreeFareTagBusiness::getType() const
  37. {
  38. return _tag->getType();
  39. }
  40. std::shared_ptr<FreeFareTag> FreeFareTagBusiness::getTag() const
  41. {
  42. return _tag;
  43. }
  44. Result<MappedKeys> FreeFareTagBusiness::mapKeys(std::vector<std::string> keys, std::function<void(int, int)> cb)
  45. {
  46. MappedKeys mappedKeys;
  47. for (int i = 0; i < 16; ++i) {
  48. std::pair<std::string, std::string> blockKeys;
  49. for (int k = 0; k < keys.size(); ++k) {
  50. auto key = keys[k];
  51. if (authenticate(i, key, MFC_KEY_A)) {
  52. blockKeys.first = key;
  53. }
  54. if (authenticate(i, key, MFC_KEY_B)) {
  55. blockKeys.second = key;
  56. }
  57. if (cb != 0) {
  58. cb((i * keys.size()) + k + 1, 16 * keys.size());
  59. }
  60. if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
  61. break;
  62. }
  63. }
  64. mappedKeys.push_back(blockKeys);
  65. }
  66. if (cb != 0) {
  67. cb(16 * keys.size(), 16 * keys.size());
  68. }
  69. return Result<MappedKeys>::ok(mappedKeys);
  70. }
  71. Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(MappedKeys keys, std::function<void(int, int)> cb)
  72. {
  73. if (keys.size() != 16) {
  74. return Result<std::vector<SectorDbo>>::error("Must have 16 sectors");
  75. }
  76. std::vector<SectorDbo> sectors;
  77. for (int s = 0; s < keys.size(); ++s) {
  78. auto sectorKey = keys[s];
  79. SectorDbo sector;
  80. bool keyA = false;
  81. bool keyB = false;
  82. for (int b = 0; b < 3; ++b) {
  83. std::string data = "";
  84. if (!sectorKey.first.empty()) {
  85. auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
  86. if (blockResult) {
  87. data = blockResult.getData();
  88. keyA = true;
  89. }
  90. }
  91. if (!sectorKey.second.empty()) {
  92. auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
  93. if (blockResult) {
  94. if (data.empty()) {
  95. data = blockResult.getData();
  96. }
  97. keyB = true;
  98. }
  99. }
  100. sector.setBlock(b, data);
  101. if (cb != 0) {
  102. cb((s * 4) + b + 1, keys.size() * 4);
  103. }
  104. }
  105. int b = 3;
  106. std::string dataA = "";
  107. std::string dataB = "";
  108. if (!sectorKey.first.empty()) {
  109. auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
  110. if (blockResult) {
  111. dataA = blockResult.getData();
  112. keyA = true;
  113. }
  114. }
  115. if (!sectorKey.second.empty()) {
  116. auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
  117. if (blockResult) {
  118. dataB = blockResult.getData();
  119. keyB = true;
  120. }
  121. }
  122. if (cb != 0) {
  123. cb((s * 4) + b + 1, keys.size() * 4);
  124. }
  125. sector.setBlock(b, dataA);
  126. AccessBitsDbo accessBitsDboA = sector.getAccessBitsDbo();
  127. sector.setBlock(b, dataB);
  128. AccessBitsDbo accessBitsDboB = sector.getAccessBitsDbo();
  129. sector.setKeyA(keyA ? sectorKey.first : "");
  130. sector.setKeyB(keyB ? sectorKey.second : "");
  131. std::string accessBits;
  132. if (keyA && accessBitsDboA.canKeyAReadAccessBitsTrailer()) {
  133. accessBits = accessBitsDboA.getBits();
  134. }
  135. else if (keyB && accessBitsDboB.canKeyBReadAccessBitsTrailer()) {
  136. accessBits = accessBitsDboB.getBits();
  137. }
  138. sector.setAccessBits(accessBits);
  139. sectors.push_back(sector);
  140. }
  141. return Result<std::vector<SectorDbo>>::ok(sectors);
  142. }
  143. Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(std::vector<std::string> keys, std::function<void(int, int)> mapCb, std::function<void(int, int)> dumpCb)
  144. {
  145. auto mappedKeysResult = mapKeys(keys, mapCb);
  146. if (!mappedKeysResult) {
  147. return Result<std::vector<SectorDbo>>::error(mappedKeysResult);
  148. }
  149. return dump(mappedKeysResult.getData(), dumpCb);
  150. }