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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. int done = 0;
  48. int total = 16 * keys.size();
  49. for (int i = 0; i < 16; ++i) {
  50. std::pair<std::string, std::string> blockKeys;
  51. for (int k = 0; k < keys.size(); ++k) {
  52. auto key = keys[k];
  53. if (authenticate(i, key, MFC_KEY_A)) {
  54. blockKeys.first = key;
  55. }
  56. if (authenticate(i, key, MFC_KEY_B)) {
  57. blockKeys.second = key;
  58. }
  59. if (cb != 0) {
  60. cb(++done, total);
  61. }
  62. if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
  63. break;
  64. }
  65. }
  66. mappedKeys.push_back(blockKeys);
  67. }
  68. if (cb != 0 && done < total) {
  69. cb(total, total);
  70. }
  71. return Result<MappedKeys>::ok(mappedKeys);
  72. }
  73. Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(MappedKeys keys, std::function<void(int, int)> cb)
  74. {
  75. if (keys.size() != 16) {
  76. return Result<std::vector<SectorDbo>>::error("Must have 16 sectors");
  77. }
  78. int done = 0;
  79. int total = 4 * keys.size();
  80. std::vector<SectorDbo> sectors;
  81. for (int s = 0; s < keys.size(); ++s) {
  82. auto sectorKey = keys[s];
  83. SectorDbo sector;
  84. bool keyA = false;
  85. bool keyB = false;
  86. for (int b = 0; b < 3; ++b) {
  87. std::string data = "";
  88. if (!sectorKey.first.empty()) {
  89. auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
  90. if (blockResult) {
  91. data = blockResult.getData();
  92. keyA = true;
  93. }
  94. }
  95. if (!sectorKey.second.empty()) {
  96. auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
  97. if (blockResult) {
  98. if (data.empty()) {
  99. data = blockResult.getData();
  100. }
  101. keyB = true;
  102. }
  103. }
  104. sector.setBlock(b, data);
  105. if (cb != 0) {
  106. cb(++done, total);
  107. }
  108. }
  109. int b = 3;
  110. std::string dataA = "";
  111. std::string dataB = "";
  112. if (!sectorKey.first.empty()) {
  113. auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
  114. if (blockResult) {
  115. dataA = blockResult.getData();
  116. keyA = true;
  117. }
  118. }
  119. if (!sectorKey.second.empty()) {
  120. auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
  121. if (blockResult) {
  122. dataB = blockResult.getData();
  123. keyB = true;
  124. }
  125. }
  126. if (cb != 0) {
  127. cb(++done, total);
  128. }
  129. sector.setBlock(b, dataA);
  130. AccessBitsDbo accessBitsDboA = sector.getAccessBitsDbo();
  131. sector.setBlock(b, dataB);
  132. AccessBitsDbo accessBitsDboB = sector.getAccessBitsDbo();
  133. sector.setKeyA(keyA ? sectorKey.first : "");
  134. sector.setKeyB(keyB ? sectorKey.second : "");
  135. std::string accessBits;
  136. if (keyA && accessBitsDboA.canKeyAReadAccessBitsTrailer()) {
  137. accessBits = accessBitsDboA.getBits();
  138. }
  139. else if (keyB && accessBitsDboB.canKeyBReadAccessBitsTrailer()) {
  140. accessBits = accessBitsDboB.getBits();
  141. }
  142. sector.setAccessBits(accessBits);
  143. sectors.push_back(sector);
  144. }
  145. if (cb != 0 && done < total) {
  146. cb(total, total);
  147. }
  148. return Result<std::vector<SectorDbo>>::ok(sectors);
  149. }
  150. Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(std::vector<std::string> keys, std::function<void(int, int)> mapCb, std::function<void(int, int)> dumpCb)
  151. {
  152. auto mappedKeysResult = mapKeys(keys, mapCb);
  153. if (!mappedKeysResult) {
  154. return Result<std::vector<SectorDbo>>::error(mappedKeysResult);
  155. }
  156. return dump(mappedKeysResult.getData(), dumpCb);
  157. }