/// <summary> /// Detects the ICC type by parsing, and analyzing the ATR /// </summary> /// <returns> /// none /// </returns> private void DetectCardType() { try { AtrInformation = AtrParser.Parse(Atr); if (AtrInformation != null && AtrInformation.HistoricalBytes.Length > 0) { DetectCard(); } } catch (Exception e) { Debug.WriteLine(e.Message + e.StackTrace); } }
/// <summary> /// Main parser method that extract information about the ATR byte array /// </summary> /// <returns> /// returns AtrInfo object if ATR is valid, null otherwise /// </returns> public static AtrInfo Parse(byte[] atrBytes) { if (atrBytes == null || atrBytes.Length < 1) { return(null); } var atrInfo = new AtrInfo(); var supportedProtocols = 0; using (var reader = new BinaryReader(new MemoryStream(atrBytes))) { var initialChar = reader.ReadByte(); if (initialChar != (byte)AtrHeader.InitialHeader) { return(null); } var formatByte = reader.ReadByte(); var interfacePresence = (byte)(formatByte.HiNibble() << 4); for (var i = 0; i < AtrInfo.MaximumAtrCodes; i++) { if ((interfacePresence & 0x10) != 0) { atrInfo.ProtocolInterfaceA[i] = reader.ReadByte(); } if ((interfacePresence & 0x20) != 0) { atrInfo.ProtocolInterfaceB[i] = reader.ReadByte(); } if ((interfacePresence & 0x40) != 0) { atrInfo.ProtocolInterfaceC[i] = reader.ReadByte(); } if ((interfacePresence & 0x80) != 0) { atrInfo.ProtocolInterfaceD[i] = reader.ReadByte(); } else { break; } interfacePresence = atrInfo.ProtocolInterfaceD[i]; supportedProtocols |= (1 << interfacePresence.LowNibble()); } var buffer = new byte[formatByte.LowNibble()]; reader.Read(buffer, 0, buffer.Length); atrInfo.HistoricalBytes = buffer; if ((supportedProtocols & ~1) != 0) { atrInfo.TckValid = ValidateTCK(atrBytes); } return(atrInfo); } }
/// <summary> /// Main parser method that extract information about the ATR byte array /// </summary> /// <returns> /// returns AtrInfo object if ATR is valid, null otherwise /// </returns> public static AtrInfo Parse(byte[] atrBytes) { if (atrBytes == null || atrBytes.Length < 1) return null; var atrInfo = new AtrInfo(); var supportedProtocols = 0; using (var reader = new BinaryReader(new MemoryStream(atrBytes))) { var initialChar = reader.ReadByte(); if (initialChar != (byte)AtrHeader.InitialHeader) { return null; } var formatByte = reader.ReadByte(); var interfacePresence = (byte)(formatByte.HiNibble() << 4); for (var i = 0; i < AtrInfo.MaximumAtrCodes; i++) { if ((interfacePresence & 0x10) != 0) atrInfo.ProtocolInterfaceA[i] = reader.ReadByte(); if ((interfacePresence & 0x20) != 0) atrInfo.ProtocolInterfaceB[i] = reader.ReadByte(); if ((interfacePresence & 0x40) != 0) atrInfo.ProtocolInterfaceC[i] = reader.ReadByte(); if ((interfacePresence & 0x80) != 0) atrInfo.ProtocolInterfaceD[i] = reader.ReadByte(); else break; interfacePresence = atrInfo.ProtocolInterfaceD[i]; supportedProtocols |= (1 << interfacePresence.LowNibble()); } var buffer = new byte[formatByte.LowNibble()]; reader.Read(buffer, 0, buffer.Length); atrInfo.HistoricalBytes = buffer; if ((supportedProtocols & ~1) != 0) { atrInfo.TckValid = ValidateTCK(atrBytes); } return atrInfo; } }