/// <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> /// <param name="stream">The stream.</param> /// <param name="len">The length.</param> /// <returns> /// Decoded Asn1Obect. /// </returns> /// <exception cref="EndOfStreamException">Unknown tag.</exception> public static Asn1Object Decode(Stream stream, int[] len) { var asn1Id = new Asn1Identifier(stream); var asn1Len = new Asn1Length(stream); var length = asn1Len.Length; len[0] = asn1Id.EncodedLength + asn1Len.EncodedLength + length; if (asn1Id.Universal == false) { return(new Asn1Tagged(stream, length, (Asn1Identifier)asn1Id.Clone())); } switch (asn1Id.Tag) { case Asn1Sequence.Tag: return(new Asn1Sequence(stream, length)); case Asn1Set.Tag: return(new Asn1Set(stream, length)); case Asn1Boolean.Tag: return(new Asn1Boolean(stream, length)); case Asn1Integer.Tag: return(new Asn1Integer(stream, length)); case Asn1OctetString.Tag: return(new Asn1OctetString(stream, length)); case Asn1Enumerated.Tag: return(new Asn1Enumerated(stream, length)); case Asn1Null.Tag: return(new Asn1Null()); // has no content to decode. default: throw new EndOfStreamException("Unknown tag"); } }
/// <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> /// <param name="stream">The stream.</param> /// <param name="len">The length.</param> /// <returns> /// Decoded Asn1Obect /// </returns> /// <exception cref="EndOfStreamException">Unknown tag</exception> public virtual Asn1Object Decode(Stream stream, int[] len) { asn1ID.Reset(stream); asn1Len.Reset(stream); var length = asn1Len.Length; len[0] = asn1ID.EncodedLength + asn1Len.EncodedLength + length; if (asn1ID.Universal == false) { return(new Asn1Tagged(this, stream, length, (Asn1Identifier)asn1ID.Clone())); } switch (asn1ID.Tag) { case Asn1Sequence.Tag: return(new Asn1Sequence(this, stream, length)); case Asn1Set.Tag: return(new Asn1Set(this, stream, length)); case Asn1Boolean.Tag: return(new Asn1Boolean(this, stream, length)); case Asn1Integer.Tag: return(new Asn1Integer(this, stream, length)); case Asn1OctetString.Tag: return(new Asn1OctetString(this, stream, length)); case Asn1Enumerated.Tag: return(new Asn1Enumerated(this, stream, length)); case Asn1Null.Tag: return(new Asn1Null()); // has no content to decode. default: throw new EndOfStreamException("Unknown tag"); // !!! need a better exception } }