-
Notifications
You must be signed in to change notification settings - Fork 8
/
cie_AtrReader.cpp
67 lines (56 loc) · 2.2 KB
/
cie_AtrReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**************************************************************************/
/*!
@file cie_AtrReader.cpp
@author Developers Italia
@license BSD (see License)
Reads and parses an Atr encoded binary content
@section HISTORY
v1.0 - Parsing of the most interesting parts
*/
/**************************************************************************/
#include "cie_AtrReader.h"
#define PN532DEBUGPRINT Serial
//Can we chain constructors in this version of C++ to avoid repetitions?
//SomeType() : SomeType(42) {}
cie_AtrReader::cie_AtrReader (cie_PN532 *cie) :
_cie(cie)
{
}
/**************************************************************************/
/*!
@brief Detects the length of an Elementary File by reading its ATR encoded content
@param filePath a structure indicating the parent Dedicated File (either ROOT_MF or CIE_DF), the selection mode (either SELECT_BY_EFID or SELECT_BY_SFI) and the file identifier (either a sfi or an efid)
@param contentLength The pointer to the length value
@returns A value indicating whether the operation succeeded or not
*/
/**************************************************************************/
bool cie_AtrReader::detectLength(const cie_EFPath filePath, word *contentLength) {
//The EF.ATR record has a minimum of 33 bytes
//Please refer to EF.ATR content here http://www.unsads.com/specs/IASECC/IAS_ECC_v1.0.1_UK.pdf#page=19
const byte chunkSize = 4;
byte endingSequence[chunkSize] = { 0x82, 0x02, 0x90, 0x00 };
byte buffer[chunkSize];
word offset = 0x21;
while (true) {
if (!_cie->readBinaryContent(filePath, buffer, offset, chunkSize)) {
*contentLength = 0;
return false;
}
byte matchingOctets = 0;
for (byte i = 0; i < chunkSize; i++) {
if (buffer[i] == endingSequence[matchingOctets]) {
matchingOctets++;
} else {
matchingOctets = 0;
}
}
if (matchingOctets == chunkSize) {
//Match found for all octets in the sequence! We're at the end of the file.
*contentLength = offset + chunkSize;
return true;
} else {
//let's position ourself in a place where we can read all of the matching octets
offset += chunkSize - matchingOctets;
}
}
}