Example #1
0
 public void Write(BerTag tag, bool value)
 {
     WriteOuterHeader(tag, 3);
     BerEncoding.EncodeTag(_output, new BerTag(BerType.Boolean));
     BerEncoding.EncodeLength(_output, 1);
     BerEncoding.EncodeBoolean(_output, value);
 }
Example #2
0
        internal override int Update()
        {
            var output      = new BerMemoryOutput();
            var implicitTag = new BerTag(BerClass.Universal, BerTypeNumber, true);

            var childrenLength = 0;

            foreach (var child in this)
            {
                childrenLength += child.IsDirty
                              ? child.Update()
                              : child.EncodedLength;
            }

            BerEncoding.EncodeTag(output, Tag.ToContainer());
            BerEncoding.EncodeLength(output, BerDefinitions.IndefiniteLength);

            BerEncoding.EncodeTag(output, implicitTag);
            BerEncoding.EncodeLength(output, BerDefinitions.IndefiniteLength);

            _encodedFrameHeader = output.ToArray();
            EncodedLength       = childrenLength + _encodedFrameHeader.Length + BerEncoding.IndefiniteLengthTerminator.Length;

            return(EncodedLength);
        }
Example #3
0
        /// <summary>
        /// Pre-encodes the BER header and returns the byte length of the encoded node.
        /// Overridden to recursively pre-encode all descendants.
        /// </summary>
        /// <returns>The length of the encoded node.</returns>
        internal override int Update()
        {
            var output      = new BerMemoryOutput();
            var implicitTag = BerType.IsApplicationDefined(BerTypeNumber)
                           ? new BerTag(BerClass.Application, BerTypeNumber & ~BerType.ApplicationFlag, true)
                           : new BerTag(BerClass.Universal, BerTypeNumber, true);

            _childrenLength = 0;

            foreach (var child in _nodes)
            {
                _childrenLength += child.IsDirty
                               ? child.Update()
                               : child.EncodedLength;
            }

            var implicitLength = _childrenLength + BerEncoding.GetHeaderLength(implicitTag, _childrenLength);

            BerEncoding.EncodeTag(output, Tag.ToContainer());
            BerEncoding.EncodeLength(output, implicitLength);

            BerEncoding.EncodeTag(output, implicitTag);
            BerEncoding.EncodeLength(output, _childrenLength);

            _encodedHeader = output.ToArray();
            EncodedLength  = _childrenLength + _encodedHeader.Length;

            return(EncodedLength);
        }
Example #4
0
        internal override void EncodeOverride(IBerOutput output)
        {
            Debug.Assert(Value != null);

            base.EncodeOverride(output);

            BerEncoding.EncodeByteArray(output, Value);
        }
Example #5
0
        internal override void EncodeOverride(IBerOutput output)
        {
            Debug.Assert(Value != null);

            base.EncodeOverride(output);

            BerEncoding.EncodeObjectIdentifier(output, Value);
        }
Example #6
0
        internal override void EncodeOverride(IBerOutput output)
        {
            Debug.Assert(Value != null);

            base.EncodeOverride(output);

            BerEncoding.EncodeRelativeOid(output, Value);
        }
Example #7
0
        public void Write(BerTag tag, long value)
        {
            var valueLength = BerEncoding.GetLongLength(value);

            WriteOuterHeader(tag, valueLength + 2);
            BerEncoding.EncodeTag(_output, new BerTag(BerType.Integer));
            BerEncoding.EncodeLength(_output, valueLength);
            BerEncoding.EncodeLong(_output, value, (int)valueLength);
        }
Example #8
0
        public void Write(BerTag tag, byte[] value)
        {
            var valueLength = value.Length;
            var innerTag    = new BerTag(BerType.OctetString);

            WriteOuterHeader(tag, valueLength + BerEncoding.GetHeaderLength(innerTag, valueLength));
            BerEncoding.EncodeTag(_output, innerTag);
            BerEncoding.EncodeLength(_output, valueLength);
            _output.WriteBytes(value);
        }
Example #9
0
        public void Write(BerTag tag, double value)
        {
            var valueOutput = new BerMemoryOutput();
            var valueLength = BerEncoding.EncodeReal(valueOutput, value);

            WriteOuterHeader(tag, valueLength + 2);
            BerEncoding.EncodeTag(_output, new BerTag(BerType.Real));
            BerEncoding.EncodeLength(_output, valueLength);
            _output.WriteBytes(valueOutput.Memory);
        }
Example #10
0
        public void WriteRelativeOid(BerTag tag, int[] value)
        {
            var innerTag    = new BerTag(BerType.RelativeOid);
            var valueLength = BerEncoding.GetRelativeOidLength(value);

            WriteOuterHeader(tag, valueLength + BerEncoding.GetHeaderLength(innerTag, valueLength));
            BerEncoding.EncodeTag(_output, innerTag);
            BerEncoding.EncodeLength(_output, valueLength);
            BerEncoding.EncodeRelativeOid(_output, value);
        }
Example #11
0
        public void Write(BerTag tag, string value)
        {
            var valueOutput = new BerMemoryOutput();
            var valueLength = BerEncoding.EncodeUtf8String(valueOutput, value);
            var innerTag    = new BerTag(BerType.UTF8String);

            WriteOuterHeader(tag, valueLength + BerEncoding.GetHeaderLength(innerTag, valueLength));
            BerEncoding.EncodeTag(_output, innerTag);
            BerEncoding.EncodeLength(_output, valueLength);
            _output.WriteBytes(valueOutput.Memory);
        }
Example #12
0
        internal override int Update()
        {
            var output = new BerMemoryOutput();

            BerEncoding.EncodeTag(output, Tag.ToContainer());
            BerEncoding.EncodeLength(output, 2);
            BerEncoding.EncodeTag(output, new BerTag(BerTypeNumber));
            BerEncoding.EncodeLength(output, 0);

            Encoded       = output.ToArray();
            EncodedLength = Encoded.Length;

            return(EncodedLength);
        }
Example #13
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));
        }
Example #14
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);
            }
        }
Example #15
0
        /// <summary>
        /// Pre-encodes the BER header and returns the byte length of the encoded node.
        /// Overriden to encode the entire TLTLV
        /// </summary>
        /// <returns>The length of the encoded node.</returns>
        internal override int Update()
        {
            var output      = new BerMemoryOutput();
            var valueLength = BerEncoding.GetLongLength(Value);

            BerEncoding.EncodeTag(output, Tag.ToContainer());
            BerEncoding.EncodeLength(output, valueLength + 2);

            BerEncoding.EncodeTag(output, new BerTag(BerType.Integer));
            BerEncoding.EncodeLength(output, valueLength);
            BerEncoding.EncodeLong(output, Value, valueLength);

            Encoded       = output.ToArray();
            EncodedLength = Encoded.Length;
            return(EncodedLength);
        }
Example #16
0
        /// <summary>
        /// Pre-encodes the BER header and returns the byte length of the encoded node.
        /// Overriden to encode the entire TLTLV
        /// </summary>
        /// <returns>The length of the encoded node.</returns>
        internal override int Update()
        {
            var output = new BerMemoryOutput();

            BerEncoding.EncodeTag(output, Tag.ToContainer());
            BerEncoding.EncodeLength(output, 3);

            BerEncoding.EncodeTag(output, new BerTag(BerType.Boolean));
            BerEncoding.EncodeLength(output, 1);
            BerEncoding.EncodeBoolean(output, Value);

            Encoded       = output.ToArray();
            EncodedLength = Encoded.Length;

            return(EncodedLength);
        }
Example #17
0
        /// <summary>
        /// Pre-encodes the BER header and returns the byte length of the encoded node.
        /// Overriden to encode the entire TLTLV
        /// </summary>
        /// <returns>The length of the encoded node.</returns>
        internal override int Update()
        {
            var output         = new BerMemoryOutput();
            var tagImplicit    = new BerTag(BerType.RelativeOid);
            var valueLength    = BerEncoding.GetRelativeOidLength(Value);
            var implicitLength = valueLength + BerEncoding.GetHeaderLength(tagImplicit, valueLength);

            BerEncoding.EncodeTag(output, Tag.ToContainer());
            BerEncoding.EncodeLength(output, implicitLength);

            BerEncoding.EncodeTag(output, tagImplicit);
            BerEncoding.EncodeLength(output, valueLength);

            Encoded = output.ToArray();

            EncodedLength = valueLength + Encoded.Length;
            return(EncodedLength);
        }
Example #18
0
        /// <summary>
        /// Pre-encodes the BER header and returns the byte length of the encoded node.
        /// Overriden to encode the entire TLTLV
        /// </summary>
        /// <returns>The length of the encoded node.</returns>
        internal override int Update()
        {
            var output      = new BerMemoryOutput();
            var arrayLength = Value.Length;

            var tagImplicit    = new BerTag(BerType.OctetString);
            var implicitLength = (arrayLength + BerEncoding.GetHeaderLength(tagImplicit, arrayLength));

            BerEncoding.EncodeTag(output, Tag.ToContainer());
            BerEncoding.EncodeLength(output, implicitLength);

            BerEncoding.EncodeTag(output, tagImplicit);
            BerEncoding.EncodeLength(output, arrayLength);

            Encoded       = output.ToArray();
            EncodedLength = arrayLength + Encoded.Length;
            return(EncodedLength);
        }
Example #19
0
        /// <summary>
        /// Pre-encodes the BER header and returns the byte length of the encoded node.
        /// </summary>
        /// <returns>The length of the encoded node.</returns>
        internal override int Update()
        {
            var output       = new BerMemoryOutput();
            var stringLength = BerEncoding.GetUtf8StringLength(Value);

            var tagImplicit    = new BerTag(BerType.UTF8String);
            var implicitLength = (stringLength + BerEncoding.GetHeaderLength(tagImplicit, stringLength));

            BerEncoding.EncodeTag(output, Tag.ToContainer());
            BerEncoding.EncodeLength(output, implicitLength);

            BerEncoding.EncodeTag(output, tagImplicit);
            BerEncoding.EncodeLength(output, stringLength);

            Encoded       = output.ToArray();
            EncodedLength = stringLength + Encoded.Length;
            return(EncodedLength);
        }
Example #20
0
        /// <summary>
        /// Pre-encodes the BER header and returns the byte length of the encoded node.
        /// Overriden to encode the entire TLTLV
        /// </summary>
        /// <returns>The length of the encoded node.</returns>
        internal override int Update()
        {
            var output = new BerMemoryOutput();

            int tagLength, innerLength = 0, valueLength;

            tagLength = BerEncoding.EncodeTag(output, Tag.ToContainer());
            BerEncoding.EncodeLength(output, 0);

            innerLength += BerEncoding.EncodeTag(output, new BerTag(BerType.Real));
            innerLength += BerEncoding.EncodeLength(output, 0);
            innerLength += valueLength = BerEncoding.EncodeReal(output, Value);

            Encoded       = output.ToArray();
            EncodedLength = Encoded.Length;

            Encoded[tagLength]     = (byte)innerLength; // fix outer length
            Encoded[tagLength + 2] = (byte)valueLength; // fix inner length
            return(EncodedLength);
        }
        bool ReadByte_Tag(byte b)
        {
            if (b == 0 && _bytesRead == 0)
            {
                ResetState(DecodeState.Terminator);
                return(false);
            }

            if (_bytesRead > 12)
            {
                ThrowError(103, "Number of tag octets out of bounds");
            }

            if (_bytesRead == 0 && (b & 0x1F) != 0x1F ||
                _bytesRead > 0 && (b & 0x80) == 0)
            {
                var input = new BerMemoryInput(_buffer);

                if (Tag.IsZero)
                {
                    // no outer tag read yet -> read outer tag
                    var tag = BerEncoding.DecodeTag(input);

                    if (tag.Class == BerClass.Universal)
                    {
                        ThrowError(107, "Universal outer tag encountered");
                    }

                    if (tag.IsContainer == false)
                    {
                        ThrowError(108, "Primitive outer tag encountered");
                    }

                    Tag = tag.ToPrimitive(); // clear container flag
                }
                else
                {
                    // outer tag already read -> read inner tag (type tag)
                    var typeTag = BerEncoding.DecodeTag(input);
                    var type    = typeTag.NumberAsType;

                    if (BerType.IsApplicationDefined(type) == false)
                    {
                        if (typeTag.Class != BerClass.Universal)
                        {
                            ThrowError(110, "Non-universal inner tag encountered");
                        }

                        if (type == 0 || type >= BerType.LastUniversal)
                        {
                            ThrowError(109, "Invalid BER type encountered");
                        }
                    }

                    IsContainer = typeTag.IsContainer;
                    Type        = type;
                }

                ResetState(DecodeState.Length);
                return(false);
            }

            _bytesRead++;
            return(false);
        }
Example #22
0
 void WriteOuterHeader(BerTag tag, int length)
 {
     BerEncoding.EncodeTag(_output, tag.ToContainer());
     BerEncoding.EncodeLength(_output, length);
 }
Example #23
0
 public void WriteContainerBegin(BerTag tag, uint type)
 {
     WriteOuterHeader(tag, BerDefinitions.IndefiniteLength);
     BerEncoding.EncodeTag(_output, new BerTag(type, true));
     BerEncoding.EncodeLength(_output, BerDefinitions.IndefiniteLength);
 }
        /// <summary>
        /// Read the next TLTLV and publishes information like <see cref="BerLib.BerReaderBase.Tag" />,
        /// <see cref="BerLib.BerReaderBase.Length" />, <see cref="BerLib.BerReaderBase.Type" /> and
        /// <see cref="BerLib.BerReaderBase.IsContainer" />.
        /// Use one of the Get() functions of BerLib.BerReaderBase to retrieve the Value.
        /// </summary>
        /// <returns>True if a valid TLTLV has been read, otherwise false.</returns>
        public bool Read()
        {
            Debug.Assert(_input != null);
            var typeTag = BerTag.Zero;

            DisposeCurrentTlv();

            if (_parentReader != null && // non top-level read?
                this.Eof)
            {
                HandleEof();
                return(false);
            }

            Tag = BerEncoding.DecodeTag(this);

            if (Tag.IsZero)
            {
                // terminator of indefinite length field: 4 zeros (outer length AND inner length must be indefinite
                if (ReadByte() == 0 &&
                    ReadByte() == 0 &&
                    ReadByte() == 0)
                {
                    HandleEof();
                    return(false);
                }
                else
                {
                    ThrowError(10, "Invalid zero tag: 0x00");
                }
            }

            OuterLength = BerEncoding.DecodeLength(this);

            if (OuterLength == 0)
            {
                ThrowError(13, "Zero outer length encountered");
            }

            if (Tag.IsContainer &&
                Tag.Class != BerClass.Universal)
            {
                typeTag = BerEncoding.DecodeTag(this);
                Length  = BerEncoding.DecodeLength(this);

                if ((Length == BerDefinitions.IndefiniteLength) != (OuterLength == BerDefinitions.IndefiniteLength))
                {
                    ThrowError(12, "Outer and inner tag must use the same length form");
                }
            }
            else
            {
                ThrowError(14, "Implicit tag or universal outer tag found");
            }

            IsContainer = typeTag.IsContainer;
            Tag         = Tag.ToPrimitive(); // clear container flag
            Type        = typeTag.NumberAsType;

            if (Length == BerDefinitions.IndefiniteLength &&
                IsContainer == false)
            {
                ThrowError(11, "Indefinite length form is only allowed on containers");
            }

            if (IsContainer == false &&
                Length > 0)
            {
                var value = new byte[Length];

                for (int index = 0; index < Length; index++)
                {
                    value[index] = ReadByte();
                }

                Value = value;
            }

            return(true);
        }
        bool ReadByte_Length(byte b)
        {
            if (_bytesExpected == 0)
            {
                if ((b & 0x80) != 0)
                {
                    _bytesExpected = (b & 0x7F) + 1;
                }
                else
                {
                    _bytesExpected = 1;
                }

                if (_bytesExpected > 5)
                {
                    ThrowError(104, "Number of length octets out of bounds");
                }
            }

            _bytesRead++;

            if (_bytesRead == _bytesExpected)
            {
                var input = new BerMemoryInput(_buffer);

                if (Type == 0)
                {
                    OuterLength = BerEncoding.DecodeLength(input);

                    if (OuterLength == 0)
                    {
                        ThrowError(102, "Zero outer length encountered");
                    }

                    ResetState(DecodeState.Tag);
                }
                else
                {
                    Length = BerEncoding.DecodeLength(input);
                    var isEofOK = Length == 0;

                    if (IsContainer)
                    {
                        ResetState(DecodeState.Tag);

                        OnNewContainer();
                        PushContainer();

                        DisposeCurrentTlv();
                        return(isEofOK);
                    }

                    if (Length == 0)
                    {
                        OnValueReady();
                    }
                    else
                    {
                        ResetState(DecodeState.Value);
                        _buffer.Capacity = (int)Length;
                    }

                    return(isEofOK);
                }
            }

            return(false);
        }