コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
0
        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());
        }
コード例 #4
0
        /// <summary>
        ///     Converts a <c>List&lt;TLVData&gt;</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());
        }
コード例 #5
0
        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());
        }
コード例 #6
0
        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());
        }
コード例 #7
0
        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));
        }
コード例 #8
0
        /// <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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        /// <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);
        }
コード例 #11
0
 /// <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"));
 }
コード例 #12
0
 /// <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);
 }