3using System.Globalization;
5using System.Threading.Tasks;
19 private readonly XmlDocument replayXml;
20 private readonly IEnumerator replayEnumerator;
22 private readonly
string mrz;
23 private bool eof =
false;
24 private bool disposed =
false;
32 : this(
XML.LoadFromFile(ReplayFileName))
43 if (ReplayXml?.DocumentElement is
null)
44 throw new ArgumentNullException(nameof(ReplayXml),
"Missing XML.");
46 if (ReplayXml.DocumentElement.Name !=
"SnifferOutput" ||
47 ReplayXml.DocumentElement.NamespaceURI !=
"http://waher.se/Schema/SnifferOutput.xsd")
49 throw new ArgumentException(
"Not a sniffer output XML file.", nameof(ReplayXml));
52 this.replayXml = ReplayXml;
53 this.replayEnumerator = this.replayXml.DocumentElement.GetEnumerator();
55 if (!this.replayEnumerator.MoveNext() ||
56 this.replayEnumerator.Current is not XmlElement E ||
57 E.LocalName !=
"Info")
59 throw new ArgumentException(
"Not an NFC replay sniffer output XML file.", nameof(ReplayXml));
62 this.mrz =
GetRows(E).Replace(
"\r\n",
"\n").Replace(
'\r',
'\n').Trim();
65 throw new ArgumentException(
"Invalid Document Information in MRZ in NFC replay sniffer output XML file.", nameof(ReplayXml));
67 this.docInfo = DocInfo;
73 public string Mrz => this.mrz;
91 this.replayEnumerator.Reset();
92 return Task.CompletedTask;
122 GC.SuppressFinalize(
this);
128 protected virtual void Dispose(
bool Disposing)
136 this.disposed =
true;
152 while (this.replayEnumerator.MoveNext())
154 if (this.replayEnumerator.Current is XmlElement E &&
155 E.LocalName ==
"Tx" &&
156 AreSame(Command,
GetBin(E)))
158 while (this.replayEnumerator.MoveNext())
160 if (this.replayEnumerator.Current is XmlElement E2 &&
161 E2.LocalName ==
"Rx")
163 return Task.FromResult(
GetBin(E2));
178 while (this.replayEnumerator.MoveNext())
180 if (this.replayEnumerator.Current is XmlElement E &&
181 E.LocalName ==
"Info")
183 string Info =
GetRows(E,
true);
185 if (Info.StartsWith(Prefix, StringComparison.InvariantCultureIgnoreCase))
186 return Info[Prefix.Length..].Trim();
198 public static byte[]
GetBin(XmlElement Event)
200 return Convert.FromBase64String(
GetRows(Event,
false));
208 public static string GetRows(XmlElement Event)
219 public static string GetRows(XmlElement Event,
bool MultiRow)
221 StringBuilder sb =
new();
223 foreach (XmlNode N
in Event.ChildNodes)
225 if (N is XmlElement E2 && E2.LocalName ==
"Row")
227 sb.Append(E2.InnerText);
233 return sb.ToString();
236 private static bool AreSame(
byte[] Bin1,
byte[] Bin2)
238 int i, c = Bin1.Length;
240 if (Bin2.Length != c)
243 for (i = 0; i < c; i++)
245 if (Bin1[i] != Bin2[i])
255 return new Exception(Message);
Class implementing the IIsoDepInterface interface for serial communication with an NFC chip,...
void SetTimeout(int Timeout)
Sets communication timeout.
void CloseIfOpen()
Closes the interface, if connected.
static string GetRows(XmlElement Event, bool MultiRow)
Gets text data from a sniffer event.
void Dispose()
Disposes the object.
Task OpenIfClosed()
Connects the interface, if not connected.
virtual void Dispose(bool Disposing)
IDisposable.Dispose
static byte[] GetBin(XmlElement Event)
Gets BASE64-encoded binary data from a sniffer event.
Task< byte[]> GetHistoricalBytes()
Return the ISO-DEP historical bytes for NfcA tags.
Task< byte[]> ExecuteCommand(byte[] Command, ICommunicationLayer CommunicationLayer)
Executes an ISO 14443-4 command on the tag.
IsoDepReplay(string ReplayFileName)
Class implementing the IIsoDepInterface interface for serial communication with an NFC chip,...
Task< byte[]> GetHighLayerResponse()
Return the higher layer response bytes for NfcB tags.
static string GetRows(XmlElement Event)
Gets text data from a sniffer event.
IsoDepReplay(XmlDocument ReplayXml)
Class implementing the IIsoDepInterface interface for serial communication with an NFC chip,...
DocumentInformation DocumentInfo
Parsed document information, based on the MRZ field found in replay file.
string Mrz
MRZ field found in replay file.
Contains MRZ-related Extensions for Machine-Readable Travel Documents.
static bool ParseMrz(string MRZ, [NotNullWhen(true)] out DocumentInformation? Info)
Derives Basic Access Control Keys from the second row of the Machine-Readable string in passport (MRZ...
Helps with common XML-related tasks.
Simple base class for classes implementing communication protocols.
void Error(string Error)
Called to inform the viewer of an error state.
void TransmitBinary(int Count)
Called when binary data has been transmitted.
ISO DEP interface, for communication with an NFC Tag.
Interface for an NFC Tag.
Interface for observable classes implementing communication protocols.