Ejemplo n.º 1
0
        /// <summary>
        /// Recursively builds a node tree from the specified reader, advancing the reader in the process.
        /// Throws an exception if the first node on the reader (which will become the tree's root cannot
        /// be decoded or is not a container.
        /// </summary>
        /// <param name="reader">The BER reader to build the tree from.</param>
        /// <param name="application">The application interface responsible for creating nodes
        /// with application-defined types. If null, containers with application-defined types
        /// will be decoded to objects of type EmberContainer.</param>
        /// <returns>The root node of the decoded tree.</returns>
        public static EmberNode Decode(EmberReader reader, EmberApplicationInterface application)
        {
            var root = null as EmberContainer;

            if (reader.Read())
            {
                root = FromReader(reader, application) as EmberContainer;

                if (root == null)
                {
                    throw new BerException(4, "Root node is not a container");
                }

                var childReader = new EmberReader(reader);

                Decode_Recurse(childReader, root, application);
            }

            return(root);
        }
Ejemplo n.º 2
0
        static void Decode_Recurse(EmberReader reader, EmberContainer parent, EmberApplicationInterface application)
        {
            while (reader.Read())
            {
                var node = FromReader(reader, application);

                if (node != null)
                {
                    var container = node as EmberContainer;

                    if (container != null)
                    {
                        var childReader = new EmberReader(reader);

                        Decode_Recurse(childReader, container, application);
                    }

                    node.ValidateAfterDecode();
                    parent.InsertChildNode(node);
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates a new instance of AsyncDomReader.
 /// </summary>
 /// <param name="application">The application interface responsible for creating nodes
 /// with application-defined types. If null, containers with application-defined types
 /// will be decoded to object of type EmberContainer.</param>
 public AsyncDomReader(EmberApplicationInterface application)
 {
     _application = application;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new EmberNode from the TLV the passed BER reader is currently positioned on.
        /// </summary>
        /// <param name="reader">The BER reader to create the node from.</param>
        /// <param name="application">The application interface responsible for creating nodes
        /// with application-defined types.</param>
        /// <returns>A new instance of EmberNode representing the read TLV or null
        /// if the TLV could not be decoded into an EmberNode.
        /// This happens if the TLV has an unsupported type.</returns>
        internal static EmberNode FromReader(BerReaderBase reader, EmberApplicationInterface application)
        {
            var node = null as EmberNode;
            var type = reader.Type;
            var tag  = reader.Tag;

            if (reader.IsContainer)
            {
                switch (type)
                {
#pragma warning disable 618 // EmberFrame is obsolete
                case BerType.Sequence:
                    node = tag == Legacy.EmberFrame.FrameTag
                         ? new Legacy.EmberFrame()
                         : new EmberSequence(tag, null);
                    break;
#pragma warning restore 618

                case BerType.Set:
                    node = new EmberSet(tag);
                    break;

                default:
                {
                    if (application != null)
                    {
                        node = application.CreateNodeFromReader(type, reader);
                    }

                    if (node == null)
                    {
                        Debug.WriteLine("WARNING: Unknown BER container type: " + type);

                        node = new EmberContainer(tag, null, type);
                    }
                    break;
                }
                }
            }
            else
            {
                switch (type)
                {
                case BerType.Boolean:
                    node = new BooleanEmberLeaf(tag, reader.GetBoolean());
                    break;

                case BerType.Integer:
                    if (reader.Length > 4)
                    {
                        node = new LongEmberLeaf(tag, reader.GetLong());
                    }
                    else
                    {
                        node = new IntegerEmberLeaf(tag, reader.GetInteger());
                    }
                    break;

                case BerType.Real:
                    node = new RealEmberLeaf(tag, reader.GetReal());
                    break;

                case BerType.UTF8String:
                    node = new StringEmberLeaf(tag, reader.GetString());
                    break;

                case BerType.OctetString:
                    node = new OctetStringEmberLeaf(tag, reader.GetOctetString());
                    break;

                case BerType.RelativeOid:
                    node = new RelativeOidEmberLeaf(tag, reader.GetRelativeOid());
                    break;

                case BerType.ObjectIdentifier:
                    node = new ObjectIdentifierEmberLeaf(tag, reader.GetObjectIdentifier());
                    break;

                case BerType.Null:
                    node = new NullEmberLeaf(tag);
                    break;

                default:
                    Debug.WriteLine("Unknown BER value type: " + type);
                    break;
                }
            }

            return(node);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Creates a new instance of AsyncDomReader.
 /// </summary>
 /// <param name="application">The application interface responsible for creating nodes
 /// with application-defined types. If null, containers with application-defined types
 /// will be decoded to object of type EmberContainer.</param>
 public AsyncDomReader(EmberApplicationInterface application)
 {
     _application = application;
 }