Esempio n. 1
0
        /// <summary>
        /// Decodes the value of the current TLV as an RelativeOid.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>An array of integers containing the subidentifiers of the RelativeOid value of the current TLV.</returns>
        public int[] GetRelativeOid()
        {
            if (IsContainer)
            {
                ThrowError(207, "Invalid RelativeOid encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.RelativeOid || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeRelativeOid(input, Length));
        }
Esempio n. 2
0
        /// <summary>
        /// Decodes the value of the current TLV a 64bit integer.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The integer value of the current TLV.</returns>
        public long GetLong()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(203, "Invalid Integer encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.Integer || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeLong(input, Length));
        }
Esempio n. 3
0
        /// <summary>
        /// Decodes the value of the current TLV as an GeneralizedTime represented
        /// as a DateTime structure.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>DateTime value containing the GeneralizedTime value of the current TLV.</returns>
        public DateTime GetGeneralizedTime()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(208, "Invalid GeneralizedTime encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.GeneralizedTime || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeGeneralizedTime(input, Length));
        }
Esempio n. 4
0
        /// <summary>
        /// Decodes the value of the current TLV as an OBJECT IDENTIFIER represented
        /// as an integer array, each integer containing one sub-identifier.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>Integer array containing the OBJECT IDENTIFIER value of the current TLV.</returns>
        public int[] GetObjectIdentifier()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(209, "Invalid ObjectIdentifier encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.ObjectIdentifier || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeObjectIdentifier(input, Length));
        }
Esempio n. 5
0
        /// <summary>
        /// Decodes the value of the current TLV as an octet string (byte array).
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>Byte array containing the OCTET STRING value of the current TLV.</returns>
        public byte[] GetOctetString()
        {
            if (IsContainer)
            {
                ThrowError(206, "Invalid OctetString encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.OctetString || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeByteArray(input, Length));
        }
Esempio n. 6
0
        /// <summary>
        /// Decodes the value of the current TLV an UTF-8 string.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The string value of the current TLV.</returns>
        public string GetString()
        {
            if (IsContainer)
            {
                ThrowError(205, "Invalid String encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.UTF8String || Type == BerType.Bitstring || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeUtf8String(input, Length));
        }
Esempio n. 7
0
        /// <summary>
        /// Decodes the value of the current TLV as boolean.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The boolean value of the current TLV.</returns>
        public bool GetBoolean()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(201, "Invalid Boolean encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.Boolean || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeBoolean(input));
        }
Esempio n. 8
0
        void AssertCodecSanity(EmberNode ember)
        {
            var originalXml = GetXml(ember);
             var output = new BerMemoryOutput();

             ember.Encode(output);

             var input = new BerMemoryInput(output.Memory);
             var reader = new EmberReader(input);

             var decoded = EmberNode.Decode(reader, new GlowApplicationInterface());
             var decodedXml = GetXml(decoded);

             if(originalXml != decodedXml)
            throw new Exception("Codec error!");
        }
Esempio n. 9
0
        /// <summary>
        /// Decodes the value of the current TLV a 64bit integer.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The integer value of the current TLV.</returns>
        public long GetLong()
        {
            if(IsContainer || Length == 0 || Value == null)
            ThrowError(203, "Invalid Integer encoding");

             Debug.Assert(Value != null || Length == 0);
             Debug.Assert(Type == BerType.Integer || BerType.IsApplicationDefined(Type));

             var input = new BerMemoryInput(Value);

             return BerEncoding.DecodeLong(input, Length);
        }
Esempio n. 10
0
        /// <summary>
        /// Decodes the value of the current TLV as an GeneralizedTime represented
        /// as a DateTime structure.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>DateTime value containing the GeneralizedTime value of the current TLV.</returns>
        public DateTime GetGeneralizedTime()
        {
            if(IsContainer || Length == 0 || Value == null)
            ThrowError(208, "Invalid GeneralizedTime encoding");

             Debug.Assert(Value != null || Length == 0);
             Debug.Assert(Type == BerType.GeneralizedTime || BerType.IsApplicationDefined(Type));

             var input = new BerMemoryInput(Value);

             return BerEncoding.DecodeGeneralizedTime(input, Length);
        }
Esempio n. 11
0
        /// <summary>
        /// Decodes the value of the current TLV as boolean.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The boolean value of the current TLV.</returns>
        public bool GetBoolean()
        {
            if(IsContainer || Length == 0 || Value == null)
            ThrowError(201, "Invalid Boolean encoding");

             Debug.Assert(Value != null || Length == 0);
             Debug.Assert(Type == BerType.Boolean || BerType.IsApplicationDefined(Type));

             var input = new BerMemoryInput(Value);

             return BerEncoding.DecodeBoolean(input);
        }
Esempio n. 12
0
        public static object[] Decode(string format, IList<byte> data)
        {
            var input = new BerMemoryInput(data);
             var objects = new List<object>();

             for(int charIndex = 0; charIndex < format.Length; charIndex++)
             {
            var ch = format[charIndex];

            switch(ch)
            {
               case '{':
                  DecodeHeader(input, TypeTags.Sequence);
                  break;

               case '}':
               {
                  if(input.ReadByte() != 0)
                     throw new BerException(4002, "Expected terminator");
                  if(input.ReadByte() != 0)
                     throw new BerException(4002, "Expected terminator");
                  break;
               }

               case 'b':
               {
                  DecodeHeader(input, TypeTags.Boolean);
                  objects.Add(BerEncoding.DecodeBoolean(input));
                  break;
               }

               case 'i':
               case 'd':
               {
                  var valueLength = DecodeHeader(input, TypeTags.Integer);
                  objects.Add(BerEncoding.DecodeInteger(input, valueLength));
                  break;
               }

               case 'l':
               case 't':
               {
                  var valueLength = DecodeHeader(input, TypeTags.Integer);
                  objects.Add(BerEncoding.DecodeLong(input, valueLength));
                  break;
               }

               case 's':
               {
                  var valueLength = DecodeHeader(input, TypeTags.UTF8String);
                  objects.Add(BerEncoding.DecodeUtf8String(input, valueLength));
                  break;
               }

               case 'y':
               {
                  var valueLength = DecodeHeader(input, TypeTags.OctetString);
                  objects.Add(BerEncoding.DecodeByteArray(input, valueLength));
                  break;
               }

               case 'f':
               case 'r':
               {
                  var valueLength = DecodeHeader(input, TypeTags.Real);
                  objects.Add(BerEncoding.DecodeReal(input, valueLength));
                  break;
               }

               case 'o':
               {
                  var valueLength = DecodeHeader(input, TypeTags.RelativeOid);
                  objects.Add(BerEncoding.DecodeRelativeOid(input, valueLength));
                  break;
               }

               case 'g':
               {
                  var valueLength = DecodeHeader(input, TypeTags.GeneralizedTime);
                  objects.Add(BerEncoding.DecodeGeneralizedTime(input, valueLength));
                  break;
               }
            }
             }

             return objects.ToArray();
        }
Esempio n. 13
0
        /// <summary>
        /// Decodes the value of the current TLV as an RelativeOid.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>An array of integers containing the subidentifiers of the RelativeOid value of the current TLV.</returns>
        public int[] GetRelativeOid()
        {
            if(IsContainer)
            ThrowError(207, "Invalid RelativeOid encoding");

             Debug.Assert(Value != null || Length == 0);
             Debug.Assert(Type == BerType.RelativeOid || BerType.IsApplicationDefined(Type));

             var input = new BerMemoryInput(Value);

             return BerEncoding.DecodeRelativeOid(input, Length);
        }
Esempio n. 14
0
        void Test_DOM()
        {
            Console.WriteLine("\r\n------------------------ DOM");

             EmberContainer container1;
             EmberContainer frame = new EmberFrame();

             container1 = new EmberSet(new BerTag(DefaultClass, 0));
             container1.Insert(new BerTag(DefaultClass, 0), -1);
             container1.Insert(new BerTag(DefaultClass, 1), 128);
             container1.Insert(new BerTag(DefaultClass, 2), -128);
             container1.Insert(new BerTag(DefaultClass, 3), 255);
             container1.Insert(new BerTag(DefaultClass, 4), -255);
             container1.Insert(new BerTag(DefaultClass, 5), 12345);
             container1.Insert(new BerTag(DefaultClass, 6), -12345);
             container1.Insert(new BerTag(DefaultClass, 7), 16384);
             container1.Insert(new BerTag(DefaultClass, 8), -16384);
             container1.Insert(new BerTag(DefaultClass, 9), 65535);
             container1.Insert(new BerTag(DefaultClass, 10), -65535);
             container1.Insert(new BerTag(DefaultClass, 11), 0);
             container1.Insert(new BerTag(DefaultClass, 12), 127);
             container1.Insert(new BerTag(DefaultClass, 13), -127);
             container1.Insert(new BerTag(DefaultClass, 1111222), 0xFFFFFFFF);
             container1.InsertOid(new BerTag(DefaultClass, 14), new int[] { 1, 3, 6, 0 });
             container1.InsertOid(new BerTag(DefaultClass, 15), new int[] { 1 });
             container1.InsertRelativeOid(new BerTag(DefaultClass, 16), new int[] { 1, 2, 3, 4, 5, 6 });
             frame.Insert(container1);

             container1 = new EmberSequence(new BerTag(DefaultClass, 1));
             container1.Insert(new BerTag(DefaultClass, 3), -0.54321);
             container1.Insert(new BerTag(DefaultClass, 5), "Wuppdich");

             var appDefined = EmberApplicationInterface.CreateApplicationDefinedSequence(new BerTag(BerClass.Application, 889), 2, container1);
             appDefined.Insert(new BerTag(DefaultClass, 100), true);

             frame.Insert(container1);

             var xml1 = GetXml(frame);

             var output = new BerMemoryOutput();
             frame.Encode(output);

             var memory = output.ToArray();
             using(var stream = new FileStream(@"N:\Temp\test.ber", FileMode.Create, FileAccess.Write))
            stream.Write(memory, 0, memory.Length);

             var input = new BerMemoryInput(memory);

             var stopwatch = new Stopwatch();
             stopwatch.Start();

             var asyncReader = new AsyncFrameReader(this);
             asyncReader.ReadBytes(output.Memory);
             var loadedFrame = asyncReader.DetachRoot();

             stopwatch.Stop();
             Console.WriteLine("load tree: {0}ms", stopwatch.ElapsedMilliseconds);

             var xml2 = GetXml(loadedFrame);

             Console.WriteLine(xml1);
             Console.WriteLine(xml2);

             Debug.Assert(xml1 == xml2);
        }
Esempio n. 15
0
        public static object[] Decode(string format, IList <byte> data)
        {
            var input   = new BerMemoryInput(data);
            var objects = new List <object>();

            for (int charIndex = 0; charIndex < format.Length; charIndex++)
            {
                var ch = format[charIndex];

                switch (ch)
                {
                case '{':
                    DecodeHeader(input, TypeTags.Sequence);
                    break;

                case '}':
                {
                    if (input.ReadByte() != 0)
                    {
                        throw new BerException(4002, "Expected terminator");
                    }
                    if (input.ReadByte() != 0)
                    {
                        throw new BerException(4002, "Expected terminator");
                    }
                    break;
                }

                case 'b':
                {
                    DecodeHeader(input, TypeTags.Boolean);
                    objects.Add(BerEncoding.DecodeBoolean(input));
                    break;
                }

                case 'i':
                case 'd':
                {
                    var valueLength = DecodeHeader(input, TypeTags.Integer);
                    objects.Add(BerEncoding.DecodeInteger(input, valueLength));
                    break;
                }

                case 'l':
                case 't':
                {
                    var valueLength = DecodeHeader(input, TypeTags.Integer);
                    objects.Add(BerEncoding.DecodeLong(input, valueLength));
                    break;
                }

                case 's':
                {
                    var valueLength = DecodeHeader(input, TypeTags.UTF8String);
                    objects.Add(BerEncoding.DecodeUtf8String(input, valueLength));
                    break;
                }

                case 'y':
                {
                    var valueLength = DecodeHeader(input, TypeTags.OctetString);
                    objects.Add(BerEncoding.DecodeByteArray(input, valueLength));
                    break;
                }

                case 'f':
                case 'r':
                {
                    var valueLength = DecodeHeader(input, TypeTags.Real);
                    objects.Add(BerEncoding.DecodeReal(input, valueLength));
                    break;
                }

                case 'o':
                {
                    var valueLength = DecodeHeader(input, TypeTags.RelativeOid);
                    objects.Add(BerEncoding.DecodeRelativeOid(input, valueLength));
                    break;
                }

                case 'g':
                {
                    var valueLength = DecodeHeader(input, TypeTags.GeneralizedTime);
                    objects.Add(BerEncoding.DecodeGeneralizedTime(input, valueLength));
                    break;
                }
                }
            }

            return(objects.ToArray());
        }
Esempio n. 16
0
        /// <summary>
        /// Decodes the value of the current TLV as an OBJECT IDENTIFIER represented
        /// as an integer array, each integer containing one sub-identifier.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>Integer array containing the OBJECT IDENTIFIER value of the current TLV.</returns>
        public int[] GetObjectIdentifier()
        {
            if(IsContainer || Length == 0 || Value == null)
            ThrowError(209, "Invalid ObjectIdentifier encoding");

             Debug.Assert(Value != null || Length == 0);
             Debug.Assert(Type == BerType.ObjectIdentifier || BerType.IsApplicationDefined(Type));

             var input = new BerMemoryInput(Value);

             return BerEncoding.DecodeObjectIdentifier(input, Length);
        }
Esempio n. 17
0
        /// <summary>
        /// Decodes the value of the current TLV as an octet string (byte array).
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>Byte array containing the OCTET STRING value of the current TLV.</returns>
        public byte[] GetOctetString()
        {
            if(IsContainer)
            ThrowError(206, "Invalid OctetString encoding");

             Debug.Assert(Value != null || Length == 0);
             Debug.Assert(Type == BerType.OctetString || BerType.IsApplicationDefined(Type));

             var input = new BerMemoryInput(Value);

             return BerEncoding.DecodeByteArray(input, Length);
        }
Esempio n. 18
0
        void Test_Real()
        {
            var values = new[] { 32.1, 32.125, 32.123, 100, 200, 300, -1000, 5.5005005, 777777777.123456789 };

             foreach(var value in values)
             {
            var output = new BerMemoryOutput();
            BerEncoding.EncodeReal(output, value);

            var input = new BerMemoryInput(output.Memory);
            var decodedValue = BerEncoding.DecodeReal(input, output.Length);

            Console.WriteLine("value={0} decoded={1}", value, decodedValue);
             }
        }
Esempio n. 19
0
        /// <summary>
        /// Decodes the value of the current TLV an UTF-8 string.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The string value of the current TLV.</returns>
        public string GetString()
        {
            if(IsContainer)
            ThrowError(205, "Invalid String encoding");

             Debug.Assert(Value != null || Length == 0);
             Debug.Assert(Type == BerType.UTF8String || Type == BerType.Bitstring || BerType.IsApplicationDefined(Type));

             var input = new BerMemoryInput(Value);

             return BerEncoding.DecodeUtf8String(input, Length);
        }
Esempio n. 20
0
        void Test_Real2()
        {
            var encoded = new byte[] { 0xC0, 0x04, 0xDF };
             var input = new BerMemoryInput(encoded);
             var decoded = BerEncoding.DecodeReal(input, encoded.Length);
             Console.WriteLine("decoded={0}", decoded);

             var output = new BerMemoryOutput();
             var reencoded = BerEncoding.EncodeReal(output, decoded);

             var bytes = output.ToArray();
             Console.WriteLine("reencoded={0}", BytesToString(bytes));
        }