Esempio n. 1
0
        private static void ParseTlv(byte[] rawTlv, ICollection <Tlv> result)
        {
            for (int i = 0, start = 0; i < rawTlv.Length; start = i)
            {
                // parse Tag
                var constructedTlv = (rawTlv[i] & 0x20) != 0;
                var moreBytes      = (rawTlv[i] & 0x1F) == 0x1F;
                while (moreBytes && (rawTlv[++i] & 0x80) != 0)
                {
                    ;
                }

                //i++
                i += 2;

                var tag = GetInt(rawTlv, start, i - start);

                //// parse Length
                //bool multiByteLength = (rawTlv[i] & 0x80) != 0;
                //int length = multiByteLength ? GetInt(rawTlv, i + 1, rawTlv[i] & 0x1F) : rawTlv[i];
                //i = multiByteLength ? i + (rawTlv[i] & 0x1F) + 1 : i + 1;
                i     += 2;
                start += 2;
                var length = GetInt(rawTlv, start, i - start);

                i += length;

                var rawData = new byte[i - start];
                Array.Copy(rawTlv, start, rawData, 0, i - start);
                var tlv = new Tlv(tag, length, rawData.Length - length, rawData);
                result.Add(tlv);

                if (constructedTlv)
                {
                    ParseTlv(tlv.Value, tlv.Children);
                }
            }
        }