Exemple #1
0
        public CommandAPDU(byte[] apdu, int apduOffset, int apduLength)
        {
            CommandAPDU commandApdu = this;

            CheckArrayBounds(apdu, apduOffset, apduLength);
            _apdu = new byte[apduLength];
            ByteCodeHelper.ArrayCopy(apdu, apduOffset, _apdu, 0, apduLength);
            Parse();
        }
Exemple #2
0
        public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data, int dataOffset, int dataLength, int ne)
        {
            CommandAPDU commandApdu = this;

            CheckArrayBounds(data, dataOffset, dataLength);
            if (dataLength > (int)ushort.MaxValue)
            {
                throw new ArgumentException("DataLength is too large");
            }
            if (ne < 0)
            {
                throw new ArgumentException("ne must not be negative");
            }
            if (ne > 65536)
            {
                throw new ArgumentException("ne is too large");
            }

            _nc = dataLength;
            if (dataLength == 0)
            {
                if (ne == 0)
                {
                    _apdu = new byte[4];
                    SetHeader(cla, ins, p1, p2);
                }
                else if (ne <= 256)
                {
                    int num = ne == 256 ? 0 : (int)(sbyte)ne;
                    _apdu = new byte[5];
                    SetHeader(cla, ins, p1, p2);
                    _apdu[4] = (byte)num;
                }
                else
                {
                    int num1;
                    int num2;
                    if (ne == 65536)
                    {
                        num1 = 0;
                        num2 = 0;
                    }
                    else
                    {
                        num1 = (int)(sbyte)(ne >> 8);
                        num2 = (int)(sbyte)ne;
                    }

                    _apdu = new byte[7];
                    SetHeader(cla, ins, p1, p2);
                    _apdu[5] = (byte)num1;
                    _apdu[6] = (byte)num2;
                }
            }
            else if (ne == 0)
            {
                if (dataLength <= (int)byte.MaxValue)
                {
                    _apdu = new byte[5 + dataLength];
                    SetHeader(cla, ins, p1, p2);
                    _apdu[4]    = (byte)dataLength;
                    _dataOffset = 5;
                    ByteCodeHelper.ArrayCopy(data, dataOffset, _apdu, 5, dataLength);
                }
                else
                {
                    _apdu = new byte[7 + dataLength];
                    SetHeader(cla, ins, p1, p2);
                    _apdu[4]    = (byte)0;
                    _apdu[5]    = (byte)(dataLength >> 8);
                    _apdu[6]    = (byte)dataLength;
                    _dataOffset = 7;
                    ByteCodeHelper.ArrayCopy(data, dataOffset, _apdu, 7, dataLength);
                }
            }
            else if (dataLength <= (int)byte.MaxValue && ne <= 256)
            {
                _apdu = new byte[6 + dataLength];
                SetHeader(cla, ins, p1, p2);
                _apdu[4]    = (byte)dataLength;
                _dataOffset = 5;
                ByteCodeHelper.ArrayCopy(data, dataOffset, _apdu, 5, dataLength);
                _apdu[_apdu.Length - 1] = ne == 256 ? (byte)0 : (byte)ne;
            }
            else
            {
                _apdu = new byte[9 + dataLength];
                SetHeader(cla, ins, p1, p2);
                _apdu[4]    = (byte)0;
                _apdu[5]    = (byte)(dataLength >> 8);
                _apdu[6]    = (byte)dataLength;
                _dataOffset = 7;
                ByteCodeHelper.ArrayCopy(data, dataOffset, _apdu, 7, dataLength);
                if (ne == 65536)
                {
                    return;
                }

                int index = _apdu.Length - 2;
                _apdu[index]     = (byte)(ne >> 8);
                _apdu[index + 1] = (byte)ne;
            }
        }