|
|
@@ -49,28 +49,28 @@ FreeFareTag::FreeFareTag(std::shared_ptr<FreeFareTagInternal> tag)
|
|
49
|
49
|
{
|
|
50
|
50
|
}
|
|
51
|
51
|
|
|
52
|
|
-LibNfc::Utils::ResultBool FreeFareTag::authenticate(int sector, std::string key, int keyType)
|
|
|
52
|
+LibNfc::Utils::ResultBool FreeFareTag::authenticate(int sector, const std::string& key, int keyType)
|
|
53
|
53
|
{
|
|
54
|
54
|
return _tag->authenticate(sector, key, keyType);
|
|
55
|
55
|
}
|
|
56
|
56
|
|
|
57
|
|
-LibNfc::Utils::Result<MappedKeys> FreeFareTag::mapKeys(std::vector<std::string> keys, std::function<void(int, int)> cb)
|
|
|
57
|
+LibNfc::Utils::Result<MappedKeys> FreeFareTag::mapKeys(const std::vector<std::string>& keys, std::function<void(int, int)> cb)
|
|
58
|
58
|
{
|
|
59
|
59
|
MappedKeys mappedKeys;
|
|
60
|
|
- int done = 0;
|
|
61
|
|
- int total = 16 * keys.size();
|
|
|
60
|
+ unsigned long done = 0;
|
|
|
61
|
+ unsigned long total = 16 * keys.size();
|
|
62
|
62
|
|
|
63
|
63
|
for (int i = 0; i < 16; ++i) {
|
|
64
|
64
|
std::pair<std::string, std::string> blockKeys;
|
|
65
|
|
- for (int k = 0; k < keys.size(); ++k) {
|
|
66
|
|
- auto key = keys[k];
|
|
|
65
|
+ for (const auto& key : keys)
|
|
|
66
|
+ {
|
|
67
|
67
|
if (authenticate(i, key, MFC_KEY_A)) {
|
|
68
|
68
|
blockKeys.first = key;
|
|
69
|
69
|
}
|
|
70
|
70
|
if (authenticate(i, key, MFC_KEY_B)) {
|
|
71
|
71
|
blockKeys.second = key;
|
|
72
|
72
|
}
|
|
73
|
|
- if (cb != 0) {
|
|
|
73
|
+ if (cb != nullptr) {
|
|
74
|
74
|
cb(++done, total);
|
|
75
|
75
|
}
|
|
76
|
76
|
if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
|
|
|
@@ -79,19 +79,19 @@ LibNfc::Utils::Result<MappedKeys> FreeFareTag::mapKeys(std::vector<std::string>
|
|
79
|
79
|
}
|
|
80
|
80
|
mappedKeys.push_back(blockKeys);
|
|
81
|
81
|
}
|
|
82
|
|
- if (cb != 0 && done < total) {
|
|
|
82
|
+ if (cb != nullptr && done < total) {
|
|
83
|
83
|
cb(total, total);
|
|
84
|
84
|
}
|
|
85
|
85
|
|
|
86
|
86
|
return LibNfc::Utils::Result<MappedKeys>::ok(mappedKeys);
|
|
87
|
87
|
}
|
|
88
|
88
|
|
|
89
|
|
-LibNfc::Utils::ResultString FreeFareTag::readBlock(int sector, int block, std::string key, int keyType)
|
|
|
89
|
+LibNfc::Utils::ResultString FreeFareTag::readBlock(int sector, int block, const std::string& key, int keyType)
|
|
90
|
90
|
{
|
|
91
|
91
|
return _tag->readBlock(sector, block, key, keyType);
|
|
92
|
92
|
}
|
|
93
|
93
|
|
|
94
|
|
-LibNfc::Utils::Result<FreeFareSector> FreeFareTag::readSector(int sector, std::string key, int keyType)
|
|
|
94
|
+LibNfc::Utils::Result<FreeFareSector> FreeFareTag::readSector(int sector, const std::string& key, int keyType)
|
|
95
|
95
|
{
|
|
96
|
96
|
std::string res;
|
|
97
|
97
|
int lastBlock = _tag->getSectorBlockCount(sector);
|
|
|
@@ -107,7 +107,7 @@ LibNfc::Utils::Result<FreeFareSector> FreeFareTag::readSector(int sector, std::s
|
|
107
|
107
|
return LibNfc::Utils::Result<FreeFareSector>::ok(FreeFareSector(res));
|
|
108
|
108
|
}
|
|
109
|
109
|
|
|
110
|
|
-LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(MappedKeys keys, std::function<void(int, int)> cb)
|
|
|
110
|
+LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(const MappedKeys& keys, std::function<void(int, int)> cb)
|
|
111
|
111
|
{
|
|
112
|
112
|
if (keys.size() != 16) {
|
|
113
|
113
|
return LibNfc::Utils::Result<std::vector<FreeFareSector>>::error("Must have 16 sectors keys");
|
|
|
@@ -139,13 +139,13 @@ LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(MappedKeys
|
|
139
|
139
|
}
|
|
140
|
140
|
}
|
|
141
|
141
|
sector.setBlock(b, data);
|
|
142
|
|
- if (cb != 0) {
|
|
|
142
|
+ if (cb != nullptr) {
|
|
143
|
143
|
cb(++done, total);
|
|
144
|
144
|
}
|
|
145
|
145
|
}
|
|
146
|
146
|
int b = 3;
|
|
147
|
|
- std::string dataA = "";
|
|
148
|
|
- std::string dataB = "";
|
|
|
147
|
+ std::string dataA;
|
|
|
148
|
+ std::string dataB;
|
|
149
|
149
|
if (!sectorKey.first.empty()) {
|
|
150
|
150
|
auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
|
|
151
|
151
|
if (blockResult) {
|
|
|
@@ -160,7 +160,7 @@ LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(MappedKeys
|
|
160
|
160
|
keyB = true;
|
|
161
|
161
|
}
|
|
162
|
162
|
}
|
|
163
|
|
- if (cb != 0) {
|
|
|
163
|
+ if (cb != nullptr) {
|
|
164
|
164
|
cb(++done, total);
|
|
165
|
165
|
}
|
|
166
|
166
|
|
|
|
@@ -182,15 +182,16 @@ LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(MappedKeys
|
|
182
|
182
|
|
|
183
|
183
|
sectors.push_back(sector);
|
|
184
|
184
|
}
|
|
185
|
|
- if (cb != 0 && done < total) {
|
|
|
185
|
+ if (cb != nullptr && done < total) {
|
|
186
|
186
|
cb(total, total);
|
|
187
|
187
|
}
|
|
188
|
188
|
|
|
189
|
189
|
return LibNfc::Utils::Result<std::vector<FreeFareSector>>::ok(sectors);
|
|
190
|
190
|
}
|
|
191
|
191
|
|
|
192
|
|
-LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(std::vector<std::string> keys, std::function<void(int, int)> mapCb,
|
|
193
|
|
- std::function<void(int, int)> readCb)
|
|
|
192
|
+LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(const std::vector<std::string>& keys,
|
|
|
193
|
+ std::function<void(int, int)> mapCb,
|
|
|
194
|
+ std::function<void(int, int)> readCb)
|
|
194
|
195
|
{
|
|
195
|
196
|
auto mappedKeysResult = mapKeys(keys, mapCb);
|
|
196
|
197
|
if (!mappedKeysResult) {
|
|
|
@@ -199,12 +200,12 @@ LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(std::vector
|
|
199
|
200
|
return read(mappedKeysResult.getData(), readCb);
|
|
200
|
201
|
}
|
|
201
|
202
|
|
|
202
|
|
-LibNfc::Utils::ResultBool FreeFareTag::writeBlock(int sector, int block, std::string key, int keyType, const std::string &data)
|
|
|
203
|
+LibNfc::Utils::ResultBool FreeFareTag::writeBlock(int sector, int block, const std::string& key, int keyType, const std::string& data)
|
|
203
|
204
|
{
|
|
204
|
205
|
return _tag->writeBlock(sector, block, key, keyType, LibNfc::Utils::StringUtils::ensureSize(data, 16));
|
|
205
|
206
|
}
|
|
206
|
207
|
|
|
207
|
|
-LibNfc::Utils::ResultBool FreeFareTag::writeSector(int sector, std::string key, int keyType, const std::string &data)
|
|
|
208
|
+LibNfc::Utils::ResultBool FreeFareTag::writeSector(int sector, const std::string& key, int keyType, const std::string& data)
|
|
208
|
209
|
{
|
|
209
|
210
|
std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 64);
|
|
210
|
211
|
std::string errors;
|
|
|
@@ -221,23 +222,23 @@ LibNfc::Utils::ResultBool FreeFareTag::writeSector(int sector, std::string key,
|
|
221
|
222
|
return LibNfc::Utils::ResultBool::error(errors);
|
|
222
|
223
|
}
|
|
223
|
224
|
|
|
224
|
|
-LibNfc::Utils::ResultBool FreeFareTag::write(MappedKeys keys, const std::string &data, bool writeSector0, std::function<void(int, int)> cb)
|
|
|
225
|
+LibNfc::Utils::ResultBool FreeFareTag::write(const MappedKeys& keys, const std::string& data, bool writeSector0, std::function<void(int, int)> cb)
|
|
225
|
226
|
{
|
|
226
|
227
|
if (keys.size() != 16) {
|
|
227
|
228
|
return LibNfc::Utils::ResultBool::error("Must have 16 sectors keys");
|
|
228
|
229
|
}
|
|
229
|
230
|
std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 1024);
|
|
230
|
231
|
std::string errors;
|
|
231
|
|
- int done = 0;
|
|
232
|
|
- int total = 4 * keys.size();
|
|
233
|
|
- for (int s = 0; s < keys.size(); ++s) {
|
|
|
232
|
+ unsigned long done = 0;
|
|
|
233
|
+ unsigned long total = 4 * keys.size();
|
|
|
234
|
+ for (unsigned long s = 0; s < keys.size(); ++s) {
|
|
234
|
235
|
auto sectorKey = keys[s];
|
|
235
|
236
|
for (int b = 0; b < 4; ++b) {
|
|
236
|
237
|
if (s == 0 && b == 0 && !writeSector0) {
|
|
237
|
238
|
continue;
|
|
238
|
239
|
}
|
|
239
|
240
|
std::string blockData = d.substr((s * 64) + (b * 16), 16);
|
|
240
|
|
- if (cb != 0 && done < total) {
|
|
|
241
|
+ if (cb != nullptr && done < total) {
|
|
241
|
242
|
bool keyA = false;
|
|
242
|
243
|
bool keyB = false;
|
|
243
|
244
|
std::string sectorErrors;
|
|
|
@@ -268,7 +269,7 @@ LibNfc::Utils::ResultBool FreeFareTag::write(MappedKeys keys, const std::string
|
|
268
|
269
|
}
|
|
269
|
270
|
}
|
|
270
|
271
|
}
|
|
271
|
|
- if (cb != 0 && done < total) {
|
|
|
272
|
+ if (cb != nullptr && done < total) {
|
|
272
|
273
|
cb(total, total);
|
|
273
|
274
|
}
|
|
274
|
275
|
if (errors.empty()) {
|
|
|
@@ -277,7 +278,7 @@ LibNfc::Utils::ResultBool FreeFareTag::write(MappedKeys keys, const std::string
|
|
277
|
278
|
return LibNfc::Utils::ResultBool::error(errors);
|
|
278
|
279
|
}
|
|
279
|
280
|
|
|
280
|
|
-LibNfc::Utils::ResultBool FreeFareTag::write(std::vector<std::string> keys, const std::string &data, bool writeSector0,
|
|
|
281
|
+LibNfc::Utils::ResultBool FreeFareTag::write(const std::vector<std::string>& keys, const std::string& data, bool writeSector0,
|
|
281
|
282
|
std::function<void(int, int)> mapCb, std::function<void(int, int)> writeCb)
|
|
282
|
283
|
{
|
|
283
|
284
|
auto mappedKeysResult = mapKeys(keys, mapCb);
|