Exemplo n.º 1
0
        private void ParseHistoricBytes(IEnumerator <byte> bytesEnumerator)
        {
            var t0 = InterfaceBytes.FirstOrDefault(tx => tx.Id == InterfaceId.T0);

            if (t0 == null)
            {
                throw new MalformedAtrException(String.Format("Missing T0 in {0}", AtrBytes.ToHexa()));
            }

            var historicLength = t0.Value & 0x0F;
            var historicBytes  = new List <byte>();

            while (bytesEnumerator.MoveNext() && historicLength > 0)
            {
                historicBytes.Add(bytesEnumerator.Current);
                historicLength--;
            }

            if (historicLength != 0)
            {
                throw new MalformedAtrException(String.Format("{0} historic bytes missing in {1}", historicLength, AtrBytes.ToHexa()));
            }

            HistoricBytes = historicBytes.ToArray();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses an ATR given by a byte array.
        /// </summary>
        /// <param name="atr"></param>
        /// <returns>Number of bytes in the ATR</returns>
        public void Parse(IEnumerable <byte> atr)
        {
            if (atr == null)
            {
                return;
            }

            AtrBytes = atr.ToArray();

            using (var bytesEnumerator = AtrBytes.AsEnumerable().GetEnumerator())
            {
                if (bytesEnumerator.MoveNext() == false)
                {
                    throw new Exception(String.Format("Invalid ATR: {0}", AtrBytes));
                }

                // TS
                Ts = new TsCharacter(bytesEnumerator.Current);

                // Interface bytes
                ParseInterfaceBytes(bytesEnumerator);

                // Historic bytes
                ParseHistoricBytes(bytesEnumerator);

                // TCK
                if (HasTck)
                {
                    Tck = bytesEnumerator.Current;
                }
            }
        }