/// <summary> /// Write TLV object to given stream. /// </summary> /// <param name="tag">TLV object</param> public static ushort GetTlvLength(ITlvTag tag) { if (tag == null) { throw new ArgumentNullException(nameof(tag)); } byte[] data = tag.EncodeValue(); bool tlv16 = tag.Type > Constants.Tlv.TypeMask || (data != null && data.Length > byte.MaxValue); if (tlv16) { if (data != null && data.Length > ushort.MaxValue) { throw new ArgumentOutOfRangeException(nameof(data)); } // first 2 bytes + 2 length bytes + data return((ushort)(4 + (data?.Length ?? 0))); } // first byte + length byte + data return((ushort)(2 + (data?.Length ?? 0))); }
/// <summary> /// Create new Integer TLV element from TLV element. /// </summary> /// <param name="tag">TLV element</param> public IntegerTag(ITlvTag tag) : base(tag) { byte[] data = tag.EncodeValue(); if (data == null) { throw new TlvException("Invalid TLV element encoded value: null."); } Value = Util.DecodeUnsignedLong(data, 0, data.Length, true); }
/// <summary> /// Create new imprint TLV element from TLV element. /// </summary> /// <param name="tag">TLV element</param> public ImprintTag(ITlvTag tag) : base(tag) { byte[] data = tag.EncodeValue(); if (data == null) { throw new TlvException("Invalid TLV element encoded value: null."); } Value = new DataHash(data); }
/// <summary> /// Write TLV object to given stream. /// </summary> /// <param name="tag">TLV object</param> public void WriteTag(ITlvTag tag) { if (tag == null) { return; } if (tag.Type > Constants.Tlv.MaxType) { throw new ArgumentOutOfRangeException(nameof(tag)); } byte[] data = tag.EncodeValue(); bool tlv16 = ((tag as TlvTag)?.ForceTlv16Encoding ?? false) || tag.Type > Constants.Tlv.TypeMask || (data != null && data.Length > byte.MaxValue); byte firstByte = (byte)((tlv16 ? Constants.Tlv.Tlv16Flag : 0) + (tag.NonCritical ? Constants.Tlv.NonCriticalFlag : 0) + (tag.Forward ? Constants.Tlv.ForwardFlag : 0)); if (tlv16) { firstByte = (byte)(firstByte | (tag.Type >> Constants.BitsInByte) & Constants.Tlv.TypeMask); Write(firstByte); Write((byte)tag.Type); if (data == null) { Write((byte)0); } else { if (data.Length > ushort.MaxValue) { throw new ArgumentOutOfRangeException(nameof(data)); } Write((byte)(data.Length >> Constants.BitsInByte)); Write((byte)data.Length); Write(data); } } else { firstByte = (byte)(firstByte | tag.Type & Constants.Tlv.TypeMask); Write(firstByte); if (data == null) { Write((byte)0); } else { Write((byte)data.Length); Write(data); } } }
/// <summary> /// Create new octet string TLV element from TLV element. /// </summary> /// <param name="tag">TLV element</param> public RawTag(ITlvTag tag) : base(tag) { byte[] data = tag.EncodeValue(); if (data == null) { throw new TlvException("Invalid TLV element encoded value: null."); } _value = data; }
/// <summary> /// Create string TLV element from TLV element. /// </summary> /// <param name="tag">TLV element</param> public StringTag(ITlvTag tag) : base(tag) { byte[] data = tag.EncodeValue(); if (data == null) { throw new TlvException("Invalid TLV element encoded value: null."); } Value = Util.DecodeNullTerminatedUtf8String(data); }
public void TlvTagBuilderGetTagByTypeTest() { RawTag child1 = new RawTag(0x1, true, false, new byte[] { 0x1, 0x2 }); RawTag child2 = new RawTag(0x2, true, false, new byte[] { 0x3, 0x4 }); CompositeTestTag tag = new CompositeTestTag(0x1, false, false, new ITlvTag[] { child1, child2 }); TlvTagBuilder builder = new TlvTagBuilder(tag); ITlvTag seachedTag = builder.GetChildByType(0x2); CollectionAssert.AreEqual(seachedTag.EncodeValue(), child2.EncodeValue(), "Invalid child returned."); }
/// <summary> /// Parse and validate the current tags. /// </summary> /// <param name="tag">TLV element that current TLV element is created from.</param> private void ParseAndValidate(ITlvTag tag) { CheckTagType(); ParseAndValidateChildTags(DecodeChildTags(tag.EncodeValue())); }