123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- //
- // Created by robin on 7/21/16.
- //
-
- #include <cstring>
- #include <iostream>
- #include "libnfc_cpptools/FreeFareAccessBits.h"
- #include "libnfc_cpptools/StringUtils.h"
- #include "libnfc_cpptools/ArrayUtils.h"
-
- namespace LibNfc
- {
- namespace FreeFare
- {
-
- const char FreeFareAccessBits::nonInvertedBitPosition[4][4] = {
- {0, 0, 0, 0 },
- {11, 10, 9, 8 },
- {23, 22, 21, 20},
- {19, 18, 17, 16}};
-
- const char FreeFareAccessBits::invertedBitPosition[4][4] = {
- {0, 0, 0, 0 },
- {7, 6, 5, 4 },
- {3, 2, 1, 0},
- {15, 14, 13, 12}};
-
- FreeFareAccessBits::FreeFareAccessBits()
- : _bits("\xff\x0f\00\x00", 4)
- {
- }
-
- FreeFareAccessBits::FreeFareAccessBits(const std::string &bits)
- : _bits(LibNfc::Utils::StringUtils::ensureSize(bits, 4))
- {
- }
-
- char FreeFareAccessBits::getUserData() const
- {
- return _bits[_bits.length() - 1];
- }
-
- void FreeFareAccessBits::setUserData(const char &data)
- {
- _bits[_bits.length() - 1] = data;
- }
-
- void FreeFareAccessBits::setBit(int i, int j, const bool& value)
- {
- char buf[_bits.length()];
- memcpy(buf, _bits.c_str(), _bits.length());
-
- LibNfc::Utils::ArrayUtils::setArrayBit(buf, nonInvertedBitPosition[i][j], value);
- LibNfc::Utils::ArrayUtils::setArrayBit(buf, invertedBitPosition[i][j], !value);
-
- _bits = std::string(buf, _bits.length());
- }
-
- bool FreeFareAccessBits::getBit(int i, int j) const
- {
- const char* buf = _bits.c_str();
- return LibNfc::Utils::ArrayUtils::getArrayBit(buf, nonInvertedBitPosition[i][j]) && !LibNfc::Utils::ArrayUtils::getArrayBit(buf, invertedBitPosition[i][j]);
- }
-
- std::string FreeFareAccessBits::getBits() const
- {
- return _bits;
- }
-
- bool FreeFareAccessBits::canKeyAReadBlock(int block) const
- {
- bool c1 = getBit(1, block);
- bool c2 = getBit(2, block);
- bool c3 = getBit(3, block);
- return !c3 || (!c1 && !c2 && c3);
- }
-
- bool FreeFareAccessBits::canKeyBReadBlock(int block) const
- {
- bool c1 = getBit(1, block);
- bool c2 = getBit(2, block);
- bool c3 = getBit(3, block);
- return !c1 || !c2 || !c3;
- }
-
- bool FreeFareAccessBits::canKeyAWriteBlock(int block) const
- {
- bool c1 = getBit(1, block);
- bool c2 = getBit(2, block);
- bool c3 = getBit(3, block);
- return !c1 && !c2 && !c3;
- }
-
- bool FreeFareAccessBits::canKeyBWriteBlock(int block) const
- {
- bool c1 = getBit(1, block);
- bool c2 = getBit(2, block);
- bool c3 = getBit(3, block);
- return (!c2 && !c3) || (c1 && !c3) || (!c1 && c2 && c3);
- }
-
- bool FreeFareAccessBits::canKeyAIncrementBlock(int block) const
- {
- bool c1 = getBit(1, block);
- bool c2 = getBit(2, block);
- bool c3 = getBit(3, block);
- return !c1 && !c2 && !c3;
- }
-
- bool FreeFareAccessBits::canKeyBIncrementBlock(int block) const
- {
- bool c1 = getBit(1, block);
- bool c2 = getBit(2, block);
- bool c3 = getBit(3, block);
- return (!c1 && !c2 && !c3) || (c1 && c2 && !c3);
- }
-
- bool FreeFareAccessBits::canKeyADecrementBlock(int block) const
- {
- bool c1 = getBit(1, block);
- bool c2 = getBit(2, block);
- bool c3 = getBit(3, block);
- return (!c1 && !c2) || (c1 && c2 && !c3);
- }
-
- bool FreeFareAccessBits::canKeyBDecrementBlock(int block) const
- {
- bool c1 = getBit(1, block);
- bool c2 = getBit(2, block);
- bool c3 = getBit(3, block);
- return (!c1 && !c2) || (c1 && c2 && !c3);
- }
-
- bool FreeFareAccessBits::canKeyAReadKeyATrailer() const
- {
- return false;
- }
-
- bool FreeFareAccessBits::canKeyBReadKeyATrailer() const
- {
- return false;
- }
-
- bool FreeFareAccessBits::canKeyAWriteKeyATrailer() const
- {
- bool c1 = getBit(1, 3);
- bool c2 = getBit(2, 3);
- bool c3 = getBit(3, 3);
- return !c1 && !c2;
- }
-
- bool FreeFareAccessBits::canKeyBWriteKeyATrailer() const
- {
- bool c1 = getBit(1, 3);
- bool c2 = getBit(2, 3);
- bool c3 = getBit(3, 3);
- return (!c1 && c2 && c3) || (c1 && !c2 && !c3);
- }
-
- bool FreeFareAccessBits::canKeyAReadAccessBitsTrailer() const
- {
- return true;
- }
-
- bool FreeFareAccessBits::canKeyBReadAccessBitsTrailer() const
- {
- bool c1 = getBit(1, 3);
- bool c2 = getBit(2, 3);
- bool c3 = getBit(3, 3);
- return c1 || (c2 && c3);
- }
-
- bool FreeFareAccessBits::canKeyAWriteAccessBitsTrailer() const
- {
- bool c1 = getBit(1, 3);
- bool c2 = getBit(2, 3);
- bool c3 = getBit(3, 3);
- return !c1 && !c2 && c3;
- }
-
- bool FreeFareAccessBits::canKeyBWriteAccessBitsTrailer() const
- {
- bool c1 = getBit(1, 3);
- bool c2 = getBit(2, 3);
- bool c3 = getBit(3, 3);
- return (!c1 && c2 && c3) || (c1 && !c2 && c3);
- }
-
- bool FreeFareAccessBits::canKeyAReadKeyBTrailer() const
- {
- bool c1 = getBit(1, 3);
- bool c2 = getBit(2, 3);
- bool c3 = getBit(3, 3);
- return (!c1 && !c2) || (!c1 && !c3);
- }
-
- bool FreeFareAccessBits::canKeyBReadKeyBTrailer() const
- {
- return false;
- }
-
- bool FreeFareAccessBits::canKeyAWriteKeyBTrailer() const
- {
- bool c1 = getBit(1, 3);
- bool c2 = getBit(2, 3);
- bool c3 = getBit(3, 3);
- return !c1 && !c2;
- }
-
- bool FreeFareAccessBits::canKeyBWriteKeyBTrailer() const
- {
- bool c1 = getBit(1, 3);
- bool c2 = getBit(2, 3);
- bool c3 = getBit(3, 3);
- return (!c1 && c2 && c3) || (c1 && !c2 && !c3);
- }
-
- }; // FreeFare
- }; // LibNfc
|