Novell.Directory.Ldap.Asn1.Asn1Length.reset C# (CSharp) Method

reset() public method

Resets an Asn1Length object by decoding data from an input stream. Note: this was added for optimization of Asn1.LBERdecoder.decode()
public reset ( System in_Renamed ) : void
in_Renamed System
return void
        public void reset(System.IO.Stream in_Renamed)
        {
            encodedLength = 0;
            int r = in_Renamed.ReadByte();
            encodedLength++;
            if (r == 0x80)
                length = - 1;
            else if (r < 0x80)
                length = r;
            else
            {
                length = 0;
                for (r = r & 0x7F; r > 0; r--)
                {
                    int part = in_Renamed.ReadByte();
                    encodedLength++;
                    if (part < 0)
                        throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in Asn1Length");
                    length = (length << 8) + part;
                }
            }
        }

Usage Example

        /// <summary>
        ///     Decode an LBER encoded value into an Asn1Object from an InputStream.
        ///     This method also returns the total length of this encoded
        ///     Asn1Object (length of type + length of length + length of content)
        ///     in the parameter len. This information is helpful when decoding
        ///     structured types.
        /// </summary>
        public virtual Asn1Object decode(Stream in_Renamed, int[] len)
        {
            asn1ID.reset(in_Renamed);
            asn1Len.reset(in_Renamed);

            var length = asn1Len.Length;

            len[0] = asn1ID.EncodedLength + asn1Len.EncodedLength + length;

            if (asn1ID.Universal)
            {
                switch (asn1ID.Tag)
                {
                case Asn1Sequence.TAG:
                    return(new Asn1Sequence(this, in_Renamed, length));

                case Asn1Set.TAG:
                    return(new Asn1Set(this, in_Renamed, length));

                case Asn1Boolean.TAG:
                    return(new Asn1Boolean(this, in_Renamed, length));

                case Asn1Integer.TAG:
                    return(new Asn1Integer(this, in_Renamed, length));

                case Asn1OctetString.TAG:
                    return(new Asn1OctetString(this, in_Renamed, length));

                case Asn1Enumerated.TAG:
                    return(new Asn1Enumerated(this, in_Renamed, length));

                case Asn1Null.TAG:
                    return(new Asn1Null());    // has no content to decode.

                /* Asn1 TYPE NOT YET SUPPORTED
                 * case Asn1BitString.TAG:
                 * return new Asn1BitString(this, in, length);
                 * case Asn1ObjectIdentifier.TAG:
                 * return new Asn1ObjectIdentifier(this, in, length);
                 * case Asn1Real.TAG:
                 * return new Asn1Real(this, in, length);
                 * case Asn1NumericString.TAG:
                 * return new Asn1NumericString(this, in, length);
                 * case Asn1PrintableString.TAG:
                 * return new Asn1PrintableString(this, in, length);
                 * case Asn1TeletexString.TAG:
                 * return new Asn1TeletexString(this, in, length);
                 * case Asn1VideotexString.TAG:
                 * return new Asn1VideotexString(this, in, length);
                 * case Asn1IA5String.TAG:
                 * return new Asn1IA5String(this, in, length);
                 * case Asn1GraphicString.TAG:
                 * return new Asn1GraphicString(this, in, length);
                 * case Asn1VisibleString.TAG:
                 * return new Asn1VisibleString(this, in, length);
                 * case Asn1GeneralString.TAG:
                 * return new Asn1GeneralString(this, in, length);
                 */


                default:
                    throw new EndOfStreamException("Unknown tag");     // !!! need a better exception
                }
            }
            // APPLICATION or CONTEXT-SPECIFIC tag
            return(new Asn1Tagged(this, in_Renamed, length, (Asn1Identifier)asn1ID.Clone()));
        }