123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- //
- // Created by robin on 7/22/16.
- //
-
- #include "libnfc_cpptools/StringUtils.h"
- #include "libnfc_cpptools/FreeFareSector.h"
-
- namespace LibNfc
- {
- namespace FreeFare
- {
-
- FreeFareSector::FreeFareSector(const std::string &data)
- {
- std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 64);
- for (int i = 0; i < 4; ++i) {
- _blocks[i] = d.substr(16 * i, 16);
- _haveBlocks[i] = true;
- }
- }
-
- const std::string &FreeFareSector::getBlock(int block) const
- {
- return _blocks[block];
- }
-
- std::string FreeFareSector::getKeyA() const
- {
- return _blocks[3].substr(0, 6);
- }
-
- std::string FreeFareSector::getKeyB() const
- {
- return _blocks[3].substr(10, 6);
- }
-
- std::string FreeFareSector::getAccessBits() const
- {
- return _blocks[3].substr(6, 4);
- }
-
- FreeFareAccessBits FreeFareSector::getAccessBitsDbo() const
- {
- return FreeFareAccessBits(getAccessBits());
- }
-
- void FreeFareSector::setBlock(int block, const std::string &data)
- {
- std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 16);
- if (block == 3) {
- setKeyA(d.substr(0, 6));
- setKeyB(d.substr(10, 6));
- setAccessBits(d.substr(6, 4));
- }
- else {
- _haveBlocks[block] = !data.empty();
- _blocks[block] = d;
- }
- }
-
- void FreeFareSector::setKeyA(const std::string &key)
- {
- std::string k = LibNfc::Utils::StringUtils::ensureSize(key, 6);
- for (int i = 0; i < k.size(); ++i) {
- _blocks[3][i] = k[i];
- }
- _hasKeyA = !key.empty();
- }
-
- void FreeFareSector::setKeyB(const std::string &key)
- {
- std::string k = LibNfc::Utils::StringUtils::ensureSize(key, 6);
- for (int i = 0; i < k.size(); ++i) {
- _blocks[3][10 + i] = k[i];
- }
- _hasKeyB = !key.empty();
- }
-
- void FreeFareSector::setAccessBits(const std::string &accessBits)
- {
- std::string a = LibNfc::Utils::StringUtils::ensureSize(accessBits, 4);
- for (int i = 0; i < a.size(); ++i) {
- _blocks[3][6 + i] = a[i];
- }
- _hasAccessBits = !accessBits.empty();
- }
-
- void FreeFareSector::setAccessBits(const FreeFareAccessBits &accessBits)
- {
- setAccessBits(accessBits.getBits());
- }
-
- bool FreeFareSector::hasBlock(int block) const
- {
- if (block == 3) {
- return _hasKeyA && _hasKeyB && _hasAccessBits;
- }
- return _haveBlocks[block];
- }
-
- bool FreeFareSector::hasKeyA() const
- {
- return _hasKeyA;
- }
-
- bool FreeFareSector::hasKeyB() const
- {
- return _hasKeyB;
- }
-
- bool FreeFareSector::hasAccessBits() const
- {
- return _hasAccessBits;
- }
-
- }; // FreeFare
- }; // LibNfc
|