/// <summary> /// Obtains and returns first TLVData found having tag <c>tagSearched</c> /// Search can be recursive /// </summary> /// <param name="tagSearched">Number of the tag to search</param> /// <param name="recursive">True to search recursively in subfields, or false</param> /// <returns>TLVData object having tag <c>tagSearched</c></returns> public TlvData GetTag(UInt32 tagSearched, Boolean recursive) { if (Tag == tagSearched) { return(this); } TlvData found = null; foreach (var subField in innerTlvs) { if ((subField.Tag == tagSearched)) { found = subField; break; } if (recursive) { var subFound = subField.GetTag(tagSearched, true); if (subFound != null) { found = subFound; break; } } } return(found); }
/// <summary> /// Converts a TLVData into an XmlNode representation, enhanced with the long name of the field found in the /// <paramref name="dictionary" /> /// </summary> /// <param name="tlv">Source data to convert</param> /// <param name="xmlDoc">XML document used to create elements</param> /// <param name="dictionary">TLV Dictionary to use to find the long name</param> /// <returns>A new XmlNode</returns> public static XElement ToXmlNode(this TlvData tlv, XDocument xmlDoc, TlvDictionary dictionary) { var xmlNode = tlv.ToXmlNode(xmlDoc); InsertDictionaryInformation(xmlNode, dictionary); InsertDictionaryInformation(xmlNode.Descendants("tlvData"), dictionary); return(xmlNode); }
public void ConstructorShortTlv() { const string data = "88 01 0F"; var tlv = new TlvData(data); Assert.AreEqual(0x88, tlv.Tag); Assert.AreEqual(0, tlv.InnerTlvs.Count); Assert.AreEqual(data, tlv.ToByteArray().ToHexa()); }
/// <summary> /// Converts a <c>List<TLVData></c> into an XML representation <see cref="string" /> /// </summary> /// <param name="tlv">Source data to convert</param> /// <returns>A new string</returns> /// <remarks> /// <example> /// <code> /// TLVData tlv = "70 03 88 01 02".toTLVData(); /// string xmltlv = tlv.toXmlString(); /// // now xmltlv is /// // <![CDATA[ /// // <?xml version="1.0" encoding="utf-16"?> /// // <tlvData tag="70" length="03" value="88 01 02"> /// // <tlvData tag="88" length="01" value="02" /> /// // </tlvData> /// // ]]> /// </code> /// </example> /// </remarks> public static string ToXmlString(this TlvData tlv) { var xs = new XmlSerializer(typeof(TlvData)); var sw = new StringWriter(); var xsns = new XmlSerializerNamespaces(); xsns.Add("", ""); xs.Serialize(sw, tlv, xsns); return(sw.ToString()); }
public void ConstructorShortTlvConstructed() { const string data = "70 07 88 01 0F 81 02 53 52"; var tlv = new TlvData(data); Assert.AreEqual(0x70, tlv.Tag); Assert.AreEqual(2, tlv.InnerTlvs.Count); Assert.AreEqual(data, tlv.ToByteArray().ToHexa()); }
public void ConstructorLongTlvConstructed() { var data = "70 81 89 88 81 82 " + new Byte[0x82].ToHexa() + " 81 02 53 52"; var tlv = new TlvData(data); Assert.AreEqual(0x70, tlv.Tag); Assert.AreEqual(0x89, tlv.Length); Assert.AreEqual(2, tlv.InnerTlvs.Count); Assert.AreEqual(0x82, tlv.InnerTlvs[0].Length); Assert.AreEqual(data, tlv.ToByteArray().ToHexa()); }
public void ConstructorLongTlv() { var data = "88 81 80 " + new Byte[0x80].ToHexa(); var tlv = new TlvData(data); Assert.AreEqual(0x88, tlv.Tag); Assert.AreEqual(0x80, tlv.Length); Assert.AreEqual(0, tlv.InnerTlvs.Count); Assert.AreEqual(data, tlv.ToByteArray().ToHexa()); Assert.AreEqual("T:88 L:8180 V:" + new Byte[0x80].ToHexa(), String.Format("{0}", tlv)); }
/// <summary> /// Parses the V (value) part of a TLV data from an array of bytes. /// </summary> /// <remarks> /// Note: length of the <c>TLVData</c> instance must be defined before calling this method. /// </remarks> /// <param name="data">Array of Bytes to be parsed</param> /// <param name="offset">Offset to begin parsing</param> /// <returns>New value of the offset (<paramref name="offset"/>+number of bytes consumed)</returns> public uint ParseV(byte[] data, uint offset) { if (IsConstructed()) { if (IsLengthUndefined) { // Value is delimited by EOC (End Of Content) = tag '00' and length '00' var tempValue = data.Skip((int)offset + 1).Take(data.Length - (int)offset - 1).ToArray(); var offsetValue = 0; var condition = true; while (condition) { var subData = new TlvData(); offsetValue = (int)subData.Parse(tempValue, (uint)offsetValue); innerTlvs.Add(subData); condition = (offsetValue < tempValue.Length) && (subData.Tag != 0x00 || subData.Length != 0x00); } value = new byte[offsetValue + 1]; Array.Copy(data, (int)offset, value, 0, offsetValue + 1); offset += (uint)(offsetValue + 1); } else { // Value is delimited by length field value = new byte[length]; Array.Copy(data, (int)offset, value, 0, (int)length); offset += length; uint offsetValue = 0; while (offsetValue < length) { var subData = new TlvData(); offsetValue = subData.Parse(value, offsetValue); innerTlvs.Add(subData); // Skip padding '00' while (offsetValue < length && value[offsetValue] == 0x00) { offsetValue++; } } } } else { value = new byte[length]; Array.Copy(data, (int)offset, value, 0, (int)length); offset += length; } return(offset); }
public static List <TlvData> ToTlvDataArray(this string buffer) { var sequence = new List <TlvData>(); var from = 0; do { var tlv = new TlvData(buffer.Substring(from)); sequence.Add(tlv); from += 2 * (int)(tlv.LengthOfT + tlv.EncodedLength.Length + tlv.Value.Length); }while (from < buffer.Length); return(sequence); }
/// <summary> /// Creates a new instance of the enhanced TLV object of <paramref name="tlv"/> object /// </summary> /// <param name="tlv">Reference <c>TLVData</c></param> /// <returns>A newly instance of the description class for the tlv</returns> public AbstractTlvObject CreateInstance(TlvData tlv) { if (tlv == null) { return(null); } var desc = Get(tlv.Tag < 0x100 ? String.Format("{0:X2}", tlv.Tag) : String.Format("{0:X4}", tlv.Tag)); if (desc == null) { return(null); } var tag = CreateInstance(desc); tag.Tlv = tlv; tag.TlvDescription = desc; return(tag); }
/// <summary> /// Converts a TLVData into an XmlNode representation /// </summary> /// <param name="tlv">Source data to convert</param> /// <param name="xmlDoc">XML document used to create elements</param> /// <returns>A new XmlNode</returns> public static XElement ToXmlNode(this TlvData tlv, XDocument xmlDoc) { xmlDoc = XDocument.Parse(tlv.ToXmlString()); return(xmlDoc.Element("tlvData")); }
/// <summary> /// Export a TLV object to XML format using a dictionnary to resolve formating /// </summary> /// <param name="tlv">Source object</param> /// <param name="dictionary">TLV Dictionary to use to find the long name</param> /// <returns>The <c>string</c> representation</returns> public static string ToXmlString(this TlvData tlv, TlvDictionary dictionary) { return(tlv.ToXmlNode(new XDocument(), dictionary).Value); }