public LcField(ApduFormat format, int payloadLength) { if (payloadLength < 0) { throw new ArgumentOutOfRangeException(nameof(payloadLength), payloadLength, $"{payloadLength} must have positive integer value."); } if (payloadLength == 0) { _lcField = new byte[0]; PayloadLength = 0; Format = LengthFieldFormat.Absent; return; } switch (format) { case ApduFormat.Short: if (payloadLength <= byte.MaxValue) { Format = LengthFieldFormat.Short; PayloadLength = payloadLength; _lcField = new[] { (byte)payloadLength }; } else { throw new ArgumentOutOfRangeException(nameof(payloadLength), payloadLength, "Short APDU maximal payload length is 255 bytes."); } break; case ApduFormat.Extended: if (payloadLength <= ushort.MaxValue) { Format = LengthFieldFormat.Extended; PayloadLength = payloadLength; _lcField = new[] { (byte)0, (byte)(payloadLength >> 8), (byte)payloadLength }; } else { throw new ArgumentOutOfRangeException(nameof(payloadLength), payloadLength, "Extended APDU maximal payload length is 65535 bytes."); } break; default: throw new ArgumentOutOfRangeException(nameof(format), format, null); } }
public LeField(ApduFormat apduFormat, LengthFieldFormat lcFieldFormat, int maximumExpectedResponseDataFieldLength) { if (maximumExpectedResponseDataFieldLength < 0) { throw new ArgumentOutOfRangeException(nameof(maximumExpectedResponseDataFieldLength), maximumExpectedResponseDataFieldLength, $"{maximumExpectedResponseDataFieldLength} must have positive integer value."); } if (maximumExpectedResponseDataFieldLength == 0) { PayloadLength = 0; Format = LengthFieldFormat.Absent; _leField = new byte[0]; return; } switch (apduFormat) { case ApduFormat.Short: if (maximumExpectedResponseDataFieldLength < byte.MaxValue + 1) { Format = LengthFieldFormat.Short; PayloadLength = maximumExpectedResponseDataFieldLength; _leField = new[] { (byte)maximumExpectedResponseDataFieldLength }; } else if (maximumExpectedResponseDataFieldLength == byte.MaxValue + 1) { Format = LengthFieldFormat.Short; PayloadLength = maximumExpectedResponseDataFieldLength; _leField = new byte[] { 0x00 }; } else { throw new ArgumentOutOfRangeException(nameof(maximumExpectedResponseDataFieldLength), maximumExpectedResponseDataFieldLength, "Short APDU maximal expected response payload length is 256 bytes."); } break; case ApduFormat.Extended: if (maximumExpectedResponseDataFieldLength < ushort.MaxValue + 1) { Format = LengthFieldFormat.Extended; PayloadLength = maximumExpectedResponseDataFieldLength; _leField = lcFieldFormat != LengthFieldFormat.Absent ? new[] { (byte)(maximumExpectedResponseDataFieldLength >> 8), (byte)maximumExpectedResponseDataFieldLength } : new[] { (byte)0, (byte)(maximumExpectedResponseDataFieldLength >> 8), (byte)maximumExpectedResponseDataFieldLength }; } else if (maximumExpectedResponseDataFieldLength == ushort.MaxValue + 1) { Format = LengthFieldFormat.Extended; PayloadLength = maximumExpectedResponseDataFieldLength; _leField = lcFieldFormat != LengthFieldFormat.Absent ? new[] { (byte)0, (byte)0 } : new[] { (byte)0, (byte)0, (byte)0 }; } else { throw new ArgumentOutOfRangeException(nameof(maximumExpectedResponseDataFieldLength), $"Maximal value exceeded. Maximal available value: {ushort.MaxValue + 1}, actual value: {maximumExpectedResponseDataFieldLength}"); } break; default: throw new ArgumentOutOfRangeException(nameof(apduFormat), apduFormat, null); } }
/// <summary> /// Initialize new Apdu command object. /// </summary> /// <param name="explicitFormat"></param> /// <param name="cla"></param> /// <param name="ins"></param> /// <param name="p1"></param> /// <param name="p2"></param> /// <param name="maximumExpectedResponseDataFieldLength">Set to null to specify maximal available response size for give apdu format.</param> public ApduCommand(ApduFormat explicitFormat, byte cla, byte ins, byte p1, byte p2, int?maximumExpectedResponseDataFieldLength = null) : this(new[] { cla, ins, p1, p2 }, new byte[0], maximumExpectedResponseDataFieldLength, format : explicitFormat) { }
/// <summary> /// Initialize new Apdu command object. /// </summary> /// <param name="explicitFormat"></param> /// <param name="cla"></param> /// <param name="ins"></param> /// <param name="p1"></param> /// <param name="p2"></param> /// <param name="payload"></param> /// <param name="maximumExpectedResponseDataFieldLength">Set to null to specify maximal available response size for give apdu format.</param> public ApduCommand(ApduFormat explicitFormat, byte cla, byte ins, byte p1, byte p2, IEnumerable <byte> payload, int?maximumExpectedResponseDataFieldLength = null) : this(new[] { cla, ins, p1, p2 }, payload, maximumExpectedResponseDataFieldLength, format : explicitFormat) { }
/// <summary> /// Initialize new Apdu command object. /// </summary> /// <param name="explicitFormat"></param> /// <param name="apduHeader"></param> /// <param name="maximumExpectedResponseDataFieldLength">Set to null to specify maximal available response size for give apdu format.</param> public ApduCommand(ApduFormat explicitFormat, IEnumerable <byte> apduHeader, int?maximumExpectedResponseDataFieldLength = null) : this(apduHeader, new byte[0], maximumExpectedResponseDataFieldLength, format : explicitFormat) { }