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.

FreeFareSector.cpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Created by robin on 7/22/16.
  3. //
  4. #include "libnfc_cpptools/StringUtils.h"
  5. #include "libnfc_cpptools/FreeFareSector.h"
  6. namespace LibNfc
  7. {
  8. namespace FreeFare
  9. {
  10. FreeFareSector::FreeFareSector(const std::string &data)
  11. {
  12. std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 64);
  13. for (int i = 0; i < 4; ++i) {
  14. _blocks[i] = d.substr(16 * i, 16);
  15. _haveBlocks[i] = true;
  16. }
  17. }
  18. const std::string &FreeFareSector::getBlock(int block) const
  19. {
  20. return _blocks[block];
  21. }
  22. std::string FreeFareSector::getKeyA() const
  23. {
  24. return _blocks[3].substr(0, 6);
  25. }
  26. std::string FreeFareSector::getKeyB() const
  27. {
  28. return _blocks[3].substr(10, 6);
  29. }
  30. std::string FreeFareSector::getAccessBits() const
  31. {
  32. return _blocks[3].substr(6, 4);
  33. }
  34. FreeFareAccessBits FreeFareSector::getAccessBitsDbo() const
  35. {
  36. return FreeFareAccessBits(getAccessBits());
  37. }
  38. void FreeFareSector::setBlock(int block, const std::string &data)
  39. {
  40. std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 16);
  41. if (block == 3) {
  42. setKeyA(d.substr(0, 6));
  43. setKeyB(d.substr(10, 6));
  44. setAccessBits(d.substr(6, 4));
  45. }
  46. else {
  47. _haveBlocks[block] = !data.empty();
  48. _blocks[block] = d;
  49. }
  50. }
  51. void FreeFareSector::setKeyA(const std::string &key)
  52. {
  53. std::string k = LibNfc::Utils::StringUtils::ensureSize(key, 6);
  54. for (int i = 0; i < k.size(); ++i) {
  55. _blocks[3][i] = k[i];
  56. }
  57. _hasKeyA = !key.empty();
  58. }
  59. void FreeFareSector::setKeyB(const std::string &key)
  60. {
  61. std::string k = LibNfc::Utils::StringUtils::ensureSize(key, 6);
  62. for (int i = 0; i < k.size(); ++i) {
  63. _blocks[3][10 + i] = k[i];
  64. }
  65. _hasKeyB = !key.empty();
  66. }
  67. void FreeFareSector::setAccessBits(const std::string &accessBits)
  68. {
  69. std::string a = LibNfc::Utils::StringUtils::ensureSize(accessBits, 4);
  70. for (int i = 0; i < a.size(); ++i) {
  71. _blocks[3][6 + i] = a[i];
  72. }
  73. _hasAccessBits = !accessBits.empty();
  74. }
  75. void FreeFareSector::setAccessBits(const FreeFareAccessBits &accessBits)
  76. {
  77. setAccessBits(accessBits.getBits());
  78. }
  79. bool FreeFareSector::hasBlock(int block) const
  80. {
  81. if (block == 3) {
  82. return _hasKeyA && _hasKeyB && _hasAccessBits;
  83. }
  84. return _haveBlocks[block];
  85. }
  86. bool FreeFareSector::hasKeyA() const
  87. {
  88. return _hasKeyA;
  89. }
  90. bool FreeFareSector::hasKeyB() const
  91. {
  92. return _hasKeyB;
  93. }
  94. bool FreeFareSector::hasAccessBits() const
  95. {
  96. return _hasAccessBits;
  97. }
  98. }; // FreeFare
  99. }; // LibNfc