Example #1
0
        /// <summary>
        /// Creates an EmberSequence object with the passed BerType. Convenience method
        /// to be called from an overridden CreateNodeFromReader method.
        /// </summary>
        protected EmberNode CreateSequence(uint type, BerReaderBase reader)
        {
            var tag  = reader.Tag;
            var node = new EmberSequence(tag, null);

            node.BerTypeNumber = type;
            return(node);
        }
Example #2
0
        /// <summary>
        /// Creates a new instance of EmberSequence with the IsOrdered flag
        /// set to true.
        /// </summary>
        /// <param name="tag">The tag of the new EmberSequence.</param>
        /// <returns>A new instance of EmberSequence with the IsOrdered flag
        /// set to true.</returns>
        public static EmberSequence CreateOrderedSequence(BerTag tag)
        {
            var sequence = new EmberSequence(tag)
            {
                IsOrdered = true,
            };

            return(sequence);
        }
Example #3
0
        /// <summary>
        /// Creates an EmberSequence object with the passed BerType. Convenience method
        /// to be called from an overridden CreateNodeFromXml method.
        /// </summary>
        protected EmberNode CreateSequence(uint type, BerTag tag, XmlReader reader)
        {
            var node = new EmberSequence(tag, null)
            {
                BerTypeNumber = type
            };

            return(node);
        }
Example #4
0
        /// <summary>
        /// Creates a container of an application-defined type with Sequence semantics
        /// </summary>
        /// <param name="type">Application-defined type. BerType.ApplicationFlag can be omitted.</param>
        public static EmberSequence CreateApplicationDefinedSequence(BerTag tag, uint type, EmberContainer parent, bool isOrdered = false)
        {
            var sequence = new EmberSequence(tag, parent)
            {
                BerTypeNumber = type | BerType.ApplicationFlag,
                IsOrdered     = isOrdered,
            };

            return(sequence);
        }
        /// <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:
                    if (tag == Legacy.EmberFrame.FrameTag)
                    {
                        Debug.WriteLine($"Warning: EmberNode / FromReader / EmberFrame is obsolete");
                        node = new Legacy.EmberFrame();
                    }
                    else
                    {
                        node = 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);
        }