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.

FreeFareAccessBits.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // Created by robin on 7/21/16.
  3. //
  4. #include <cstring>
  5. #include <iostream>
  6. #include "libnfc_cpptools/FreeFareAccessBits.h"
  7. #include "libnfc_cpptools/StringUtils.h"
  8. #include "libnfc_cpptools/ArrayUtils.h"
  9. namespace LibNfc
  10. {
  11. namespace FreeFare
  12. {
  13. const char FreeFareAccessBits::nonInvertedBitPosition[4][4] = {
  14. {0, 0, 0, 0 },
  15. {11, 10, 9, 8 },
  16. {23, 22, 21, 20},
  17. {19, 18, 17, 16}};
  18. const char FreeFareAccessBits::invertedBitPosition[4][4] = {
  19. {0, 0, 0, 0 },
  20. {7, 6, 5, 4 },
  21. {3, 2, 1, 0},
  22. {15, 14, 13, 12}};
  23. FreeFareAccessBits::FreeFareAccessBits()
  24. : _bits("\xff\x0f\00\x00", 4)
  25. {
  26. }
  27. FreeFareAccessBits::FreeFareAccessBits(const std::string &bits)
  28. : _bits(LibNfc::Utils::StringUtils::ensureSize(bits, 4))
  29. {
  30. }
  31. char FreeFareAccessBits::getUserData() const
  32. {
  33. return _bits[_bits.length() - 1];
  34. }
  35. void FreeFareAccessBits::setUserData(const char &data)
  36. {
  37. _bits[_bits.length() - 1] = data;
  38. }
  39. void FreeFareAccessBits::setBit(int i, int j, const bool& value)
  40. {
  41. char buf[_bits.length()];
  42. memcpy(buf, _bits.c_str(), _bits.length());
  43. LibNfc::Utils::ArrayUtils::setArrayBit(buf, nonInvertedBitPosition[i][j], value);
  44. LibNfc::Utils::ArrayUtils::setArrayBit(buf, invertedBitPosition[i][j], !value);
  45. _bits = std::string(buf, _bits.length());
  46. }
  47. bool FreeFareAccessBits::getBit(int i, int j) const
  48. {
  49. const char* buf = _bits.c_str();
  50. return LibNfc::Utils::ArrayUtils::getArrayBit(buf, nonInvertedBitPosition[i][j]) && !LibNfc::Utils::ArrayUtils::getArrayBit(buf, invertedBitPosition[i][j]);
  51. }
  52. std::string FreeFareAccessBits::getBits() const
  53. {
  54. return _bits;
  55. }
  56. bool FreeFareAccessBits::canKeyAReadBlock(int block) const
  57. {
  58. bool c1 = getBit(1, block);
  59. bool c2 = getBit(2, block);
  60. bool c3 = getBit(3, block);
  61. return !c3 || (!c1 && !c2 && c3);
  62. }
  63. bool FreeFareAccessBits::canKeyBReadBlock(int block) const
  64. {
  65. bool c1 = getBit(1, block);
  66. bool c2 = getBit(2, block);
  67. bool c3 = getBit(3, block);
  68. return !c1 || !c2 || !c3;
  69. }
  70. bool FreeFareAccessBits::canKeyAWriteBlock(int block) const
  71. {
  72. bool c1 = getBit(1, block);
  73. bool c2 = getBit(2, block);
  74. bool c3 = getBit(3, block);
  75. return !c1 && !c2 && !c3;
  76. }
  77. bool FreeFareAccessBits::canKeyBWriteBlock(int block) const
  78. {
  79. bool c1 = getBit(1, block);
  80. bool c2 = getBit(2, block);
  81. bool c3 = getBit(3, block);
  82. return (!c2 && !c3) || (c1 && !c3) || (!c1 && c2 && c3);
  83. }
  84. bool FreeFareAccessBits::canKeyAIncrementBlock(int block) const
  85. {
  86. bool c1 = getBit(1, block);
  87. bool c2 = getBit(2, block);
  88. bool c3 = getBit(3, block);
  89. return !c1 && !c2 && !c3;
  90. }
  91. bool FreeFareAccessBits::canKeyBIncrementBlock(int block) const
  92. {
  93. bool c1 = getBit(1, block);
  94. bool c2 = getBit(2, block);
  95. bool c3 = getBit(3, block);
  96. return (!c1 && !c2 && !c3) || (c1 && c2 && !c3);
  97. }
  98. bool FreeFareAccessBits::canKeyADecrementBlock(int block) const
  99. {
  100. bool c1 = getBit(1, block);
  101. bool c2 = getBit(2, block);
  102. bool c3 = getBit(3, block);
  103. return (!c1 && !c2) || (c1 && c2 && !c3);
  104. }
  105. bool FreeFareAccessBits::canKeyBDecrementBlock(int block) const
  106. {
  107. bool c1 = getBit(1, block);
  108. bool c2 = getBit(2, block);
  109. bool c3 = getBit(3, block);
  110. return (!c1 && !c2) || (c1 && c2 && !c3);
  111. }
  112. bool FreeFareAccessBits::canKeyAReadKeyATrailer() const
  113. {
  114. return false;
  115. }
  116. bool FreeFareAccessBits::canKeyBReadKeyATrailer() const
  117. {
  118. return false;
  119. }
  120. bool FreeFareAccessBits::canKeyAWriteKeyATrailer() const
  121. {
  122. bool c1 = getBit(1, 3);
  123. bool c2 = getBit(2, 3);
  124. bool c3 = getBit(3, 3);
  125. return !c1 && !c2;
  126. }
  127. bool FreeFareAccessBits::canKeyBWriteKeyATrailer() const
  128. {
  129. bool c1 = getBit(1, 3);
  130. bool c2 = getBit(2, 3);
  131. bool c3 = getBit(3, 3);
  132. return (!c1 && c2 && c3) || (c1 && !c2 && !c3);
  133. }
  134. bool FreeFareAccessBits::canKeyAReadAccessBitsTrailer() const
  135. {
  136. return true;
  137. }
  138. bool FreeFareAccessBits::canKeyBReadAccessBitsTrailer() const
  139. {
  140. bool c1 = getBit(1, 3);
  141. bool c2 = getBit(2, 3);
  142. bool c3 = getBit(3, 3);
  143. return c1 || (c2 && c3);
  144. }
  145. bool FreeFareAccessBits::canKeyAWriteAccessBitsTrailer() const
  146. {
  147. bool c1 = getBit(1, 3);
  148. bool c2 = getBit(2, 3);
  149. bool c3 = getBit(3, 3);
  150. return !c1 && !c2 && c3;
  151. }
  152. bool FreeFareAccessBits::canKeyBWriteAccessBitsTrailer() const
  153. {
  154. bool c1 = getBit(1, 3);
  155. bool c2 = getBit(2, 3);
  156. bool c3 = getBit(3, 3);
  157. return (!c1 && c2 && c3) || (c1 && !c2 && c3);
  158. }
  159. bool FreeFareAccessBits::canKeyAReadKeyBTrailer() const
  160. {
  161. bool c1 = getBit(1, 3);
  162. bool c2 = getBit(2, 3);
  163. bool c3 = getBit(3, 3);
  164. return (!c1 && !c2) || (!c1 && !c3);
  165. }
  166. bool FreeFareAccessBits::canKeyBReadKeyBTrailer() const
  167. {
  168. return false;
  169. }
  170. bool FreeFareAccessBits::canKeyAWriteKeyBTrailer() const
  171. {
  172. bool c1 = getBit(1, 3);
  173. bool c2 = getBit(2, 3);
  174. bool c3 = getBit(3, 3);
  175. return !c1 && !c2;
  176. }
  177. bool FreeFareAccessBits::canKeyBWriteKeyBTrailer() const
  178. {
  179. bool c1 = getBit(1, 3);
  180. bool c2 = getBit(2, 3);
  181. bool c3 = getBit(3, 3);
  182. return (!c1 && c2 && c3) || (c1 && !c2 && !c3);
  183. }
  184. }; // FreeFare
  185. }; // LibNfc