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 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // Created by robin on 7/22/16.
  3. //
  4. #include <DBO/StringUtils.h>
  5. #include "FreeFareTagBusiness.h"
  6. FreeFareTagBusiness::FreeFareTagBusiness(std::shared_ptr<FreeFareTag> tag)
  7. : _tag(tag)
  8. {
  9. }
  10. ResultBool FreeFareTagBusiness::authenticate(int sector, std::string key, int keyType)
  11. {
  12. return _tag->authenticate(sector, key, keyType);
  13. }
  14. Result<MappedKeys> FreeFareTagBusiness::mapKeys(std::vector<std::string> keys, std::function<void(int, int)> cb)
  15. {
  16. MappedKeys mappedKeys;
  17. int done = 0;
  18. int total = 16 * keys.size();
  19. for (int i = 0; i < 16; ++i) {
  20. std::pair<std::string, std::string> blockKeys;
  21. for (int k = 0; k < keys.size(); ++k) {
  22. auto key = keys[k];
  23. if (authenticate(i, key, MFC_KEY_A)) {
  24. blockKeys.first = key;
  25. }
  26. if (authenticate(i, key, MFC_KEY_B)) {
  27. blockKeys.second = key;
  28. }
  29. if (cb != 0) {
  30. cb(++done, total);
  31. }
  32. if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
  33. break;
  34. }
  35. }
  36. mappedKeys.push_back(blockKeys);
  37. }
  38. if (cb != 0 && done < total) {
  39. cb(total, total);
  40. }
  41. return Result<MappedKeys>::ok(mappedKeys);
  42. }
  43. ResultString FreeFareTagBusiness::readBlock(int sector, int block, std::string key, int keyType)
  44. {
  45. return _tag->readBlock(sector, block, key, keyType);
  46. }
  47. Result<SectorDbo> FreeFareTagBusiness::readSector(int sector, std::string key, int keyType)
  48. {
  49. std::string res;
  50. int lastBlock = _tag->getSectorBlockCount(sector);
  51. for (int i = 0; i < lastBlock; ++i) {
  52. auto data = readBlock(sector, i, key, keyType);
  53. if (data) {
  54. res += data.getData();
  55. }
  56. else {
  57. return Result<SectorDbo>::error(data);
  58. }
  59. }
  60. return Result<SectorDbo>::ok(SectorDbo(res));
  61. }
  62. Result<std::vector<SectorDbo>> FreeFareTagBusiness::read(MappedKeys keys, std::function<void(int, int)> cb)
  63. {
  64. if (keys.size() != 16) {
  65. return Result<std::vector<SectorDbo>>::error("Must have 16 sectors keys");
  66. }
  67. int done = 0;
  68. int total = 4 * keys.size();
  69. std::vector<SectorDbo> sectors;
  70. for (int s = 0; s < keys.size(); ++s) {
  71. auto sectorKey = keys[s];
  72. SectorDbo sector;
  73. bool keyA = false;
  74. bool keyB = false;
  75. for (int b = 0; b < 3; ++b) {
  76. std::string data = "";
  77. if (!sectorKey.first.empty()) {
  78. auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
  79. if (blockResult) {
  80. data = blockResult.getData();
  81. keyA = true;
  82. }
  83. }
  84. if (!sectorKey.second.empty()) {
  85. auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
  86. if (blockResult) {
  87. if (data.empty()) {
  88. data = blockResult.getData();
  89. }
  90. keyB = true;
  91. }
  92. }
  93. sector.setBlock(b, data);
  94. if (cb != 0) {
  95. cb(++done, total);
  96. }
  97. }
  98. int b = 3;
  99. std::string dataA = "";
  100. std::string dataB = "";
  101. if (!sectorKey.first.empty()) {
  102. auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
  103. if (blockResult) {
  104. dataA = blockResult.getData();
  105. keyA = true;
  106. }
  107. }
  108. if (!sectorKey.second.empty()) {
  109. auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
  110. if (blockResult) {
  111. dataB = blockResult.getData();
  112. keyB = true;
  113. }
  114. }
  115. if (cb != 0) {
  116. cb(++done, total);
  117. }
  118. sector.setBlock(b, dataA);
  119. AccessBitsDbo accessBitsDboA = sector.getAccessBitsDbo();
  120. sector.setBlock(b, dataB);
  121. AccessBitsDbo accessBitsDboB = sector.getAccessBitsDbo();
  122. sector.setKeyA(keyA ? sectorKey.first : "");
  123. sector.setKeyB(keyB ? sectorKey.second : "");
  124. std::string accessBits;
  125. if (keyA && accessBitsDboA.canKeyAReadAccessBitsTrailer()) {
  126. accessBits = accessBitsDboA.getBits();
  127. }
  128. else if (keyB && accessBitsDboB.canKeyBReadAccessBitsTrailer()) {
  129. accessBits = accessBitsDboB.getBits();
  130. }
  131. sector.setAccessBits(accessBits);
  132. sectors.push_back(sector);
  133. }
  134. if (cb != 0 && done < total) {
  135. cb(total, total);
  136. }
  137. return Result<std::vector<SectorDbo>>::ok(sectors);
  138. }
  139. Result<std::vector<SectorDbo>> FreeFareTagBusiness::read(std::vector<std::string> keys, std::function<void(int, int)> mapCb,
  140. std::function<void(int, int)> readCb)
  141. {
  142. auto mappedKeysResult = mapKeys(keys, mapCb);
  143. if (!mappedKeysResult) {
  144. return Result<std::vector<SectorDbo>>::error(mappedKeysResult);
  145. }
  146. return read(mappedKeysResult.getData(), readCb);
  147. }
  148. ResultBool FreeFareTagBusiness::writeBlock(int sector, int block, std::string key, int keyType, const std::string &data)
  149. {
  150. return _tag->writeBlock(sector, block, key, keyType, StringUtils::ensureSize(data, 16));
  151. }
  152. ResultBool FreeFareTagBusiness::writeSector(int sector, std::string key, int keyType, const std::string &data)
  153. {
  154. std::string d = StringUtils::ensureSize(data, 64);
  155. std::string errors;
  156. for (int b = 0; b < 4; ++b) {
  157. auto result = writeBlock(sector, b, key, keyType, d.substr(b * 16, 16));
  158. if (!result) {
  159. errors += std::string(errors.empty() ? "" : "\n") + "Failed to write sector " + std::to_string(sector) +
  160. " block " + std::to_string(b) + " with key " + (keyType == MFC_KEY_A ? "A" : "B") + ": " + result.getError();
  161. }
  162. }
  163. if (errors.empty()) {
  164. return ResultBool::ok(true);
  165. }
  166. return ResultBool::error(errors);
  167. }
  168. ResultBool FreeFareTagBusiness::write(MappedKeys keys, const std::string &data, bool writeSector0, std::function<void(int, int)> cb)
  169. {
  170. if (keys.size() != 16) {
  171. return ResultBool::error("Must have 16 sectors keys");
  172. }
  173. std::string d = StringUtils::ensureSize(data, 1024);
  174. std::string errors;
  175. int done = 0;
  176. int total = 4 * (keys.size() - (writeSector0 ? 0 : 1));
  177. for (int s = 0; s < keys.size(); ++s) {
  178. auto sectorKey = keys[s];
  179. for (int b = (s == 0 && !writeSector0) ? 1 : 0; b < 4; ++b) {
  180. std::string blockData = d.substr((s * 64) + (b * 16), 16);
  181. if (cb != 0 && done < total) {
  182. bool keyA = false;
  183. bool keyB = false;
  184. std::string sectorErrors;
  185. if (!sectorKey.first.empty()) {
  186. auto resultA = writeBlock(s, b, sectorKey.first, MFC_KEY_A, blockData);
  187. if (resultA) {
  188. keyA = true;
  189. }
  190. else {
  191. sectorErrors = "Failed to write sector " + std::to_string(s) +
  192. " block " + std::to_string(b) + " with key A: " + resultA.getError();
  193. }
  194. }
  195. if (!keyA && !sectorKey.second.empty()) {
  196. auto resultB = writeBlock(s, b, sectorKey.first, MFC_KEY_A, blockData);
  197. if (resultB) {
  198. keyB = true;
  199. }
  200. else {
  201. sectorErrors = std::string(sectorErrors.empty() ? "" : "\n") + "Failed to write sector " + std::to_string(s) +
  202. " block " + std::to_string(b) + " with key B: " + resultB.getError();
  203. }
  204. }
  205. if (!keyA && !keyB) {
  206. errors += std::string(errors.empty() ? "" : "\n") + sectorErrors;
  207. }
  208. cb(++done, total);
  209. }
  210. }
  211. }
  212. if (cb != 0 && done < total) {
  213. cb(total, total);
  214. }
  215. if (errors.empty()) {
  216. return ResultBool::ok(true);
  217. }
  218. return ResultBool::error(errors);
  219. }
  220. ResultBool FreeFareTagBusiness::write(std::vector<std::string> keys, const std::string &data, bool writeSector0,
  221. std::function<void(int, int)> mapCb, std::function<void(int, int)> writeCb)
  222. {
  223. auto mappedKeysResult = mapKeys(keys, mapCb);
  224. if (!mappedKeysResult) {
  225. return ResultBool::error(mappedKeysResult);
  226. }
  227. return write(mappedKeysResult.getData(), data, writeSector0, writeCb);
  228. }
  229. const std::string &FreeFareTagBusiness::getUid() const
  230. {
  231. return _tag->getUid();
  232. }
  233. freefare_tag_type FreeFareTagBusiness::getType() const
  234. {
  235. return _tag->getType();
  236. }
  237. std::shared_ptr<FreeFareTag> FreeFareTagBusiness::getTag() const
  238. {
  239. return _tag;
  240. }