/* Asn1 TYPE NOT YET SUPPORTED * Encode an Asn1ObjectIdentifier directly to a stream. * public void encode(Asn1ObjectIdentifier oi, OutputStream out) * throws IOException * { * throw new IOException("LBEREncoder: Encode to a stream not implemented"); * } */ /* Asn1 TYPE NOT YET SUPPORTED * Encode an Asn1CharacterString directly to a stream. * public void encode(Asn1CharacterString cs, OutputStream out) * throws IOException * { * throw new IOException("LBEREncoder: Encode to a stream not implemented"); * } */ /* Encoders for ASN.1 structured types */ /// <summary> /// Encode an Asn1Structured into the specified outputstream. This method /// can be used to encode SET, SET_OF, SEQUENCE, SEQUENCE_OF. /// </summary> public void Encode(Asn1Structured c, Stream outRenamed) { Encode(c.GetIdentifier(), outRenamed); var valueRenamed = c.ToArray(); var output = new MemoryStream(); /* Cycle through each element encoding each element */ for (var i = 0; i < valueRenamed.Length; i++) { valueRenamed[i].Encode(this, output); } /* Encode the length */ EncodeLength((int)output.Length, outRenamed); /* Add each encoded element into the output stream */ var array = output.ToArray(); outRenamed.Write(array, 0, array.Length); }
/* Asn1 TYPE NOT YET SUPPORTED * Encode an Asn1ObjectIdentifier directly to a stream. * public void encode(Asn1ObjectIdentifier oi, OutputStream out) * throws IOException * { * throw new IOException("LBEREncoder: Encode to a stream not implemented"); * } */ /* Asn1 TYPE NOT YET SUPPORTED * Encode an Asn1CharacterString directly to a stream. * public void encode(Asn1CharacterString cs, OutputStream out) * throws IOException * { * throw new IOException("LBEREncoder: Encode to a stream not implemented"); * } */ /* Encoders for ASN.1 structured types */ /// <summary> Encode an Asn1Structured into the specified outputstream. This method /// can be used to encode SET, SET_OF, SEQUENCE, SEQUENCE_OF /// </summary> public void encode(Asn1Structured c, System.IO.Stream out_Renamed) { encode(c.getIdentifier(), out_Renamed); Asn1Object[] value_Renamed = c.toArray(); MemoryStream output = new MemoryStream(); /* Cycle through each element encoding each element */ for (int i = 0; i < value_Renamed.Length; i++) { (value_Renamed[i]).encode(this, output); } /* Encode the length */ encodeLength((int)output.Length, out_Renamed); /* Add each encoded element into the output stream */ sbyte[] temp_sbyteArray; temp_sbyteArray = SupportClass.ToSByteArray(output.ToArray()); out_Renamed.Write(SupportClass.ToByteArray(temp_sbyteArray), 0, temp_sbyteArray.Length);;; }
/* Asn1 TYPE NOT YET SUPPORTED * Encode an Asn1ObjectIdentifier directly to a stream. * public void encode(Asn1ObjectIdentifier oi, OutputStream out) * throws IOException * { * throw new IOException("LBEREncoder: Encode to a stream not implemented"); * } */ /* Asn1 TYPE NOT YET SUPPORTED * Encode an Asn1CharacterString directly to a stream. * public void encode(Asn1CharacterString cs, OutputStream out) * throws IOException * { * throw new IOException("LBEREncoder: Encode to a stream not implemented"); * } */ /* Encoders for ASN.1 structured types */ /// <summary> Encode an Asn1Structured into the specified outputstream. This method /// can be used to encode SET, SET_OF, SEQUENCE, SEQUENCE_OF /// </summary> public void encode(Asn1Structured c, System.IO.Stream out_Renamed) { encode(c.getIdentifier(), out_Renamed); Asn1Object[] value_Renamed = c.toArray(); System.IO.MemoryStream output = new System.IO.MemoryStream(); /* Cycle through each element encoding each element */ for (int i = 0; i < value_Renamed.Length; i++) { (value_Renamed[i]).encode(this, output); } /* Encode the length */ encodeLength((int)output.Length, out_Renamed); /* Add each encoded element into the output stream */ sbyte[] temp_sbyteArray; temp_sbyteArray = SupportClass.ToSByteArray(output.ToArray()); out_Renamed.Write(SupportClass.ToByteArray(temp_sbyteArray), 0, temp_sbyteArray.Length);;; return ; }
/// <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 async ValueTask <Asn1Object> Decode(Stream inRenamed, int[] len, CancellationToken cancellationToken) { _asn1Id.Reset(inRenamed); _asn1Len.Reset(inRenamed); var length = _asn1Len.Length; len[0] = _asn1Id.EncodedLength + _asn1Len.EncodedLength + length; if (_asn1Id.IsUniversal) { switch (_asn1Id.Tag) { case Asn1Sequence.Tag: return(new Asn1Sequence(await Asn1Structured.DecodeStructured(this, inRenamed, length, cancellationToken).ConfigureAwait(false))); case Asn1Set.Tag: return(new Asn1Set(await Asn1Structured.DecodeStructured(this, inRenamed, length, cancellationToken).ConfigureAwait(false))); case Asn1Boolean.Tag: return(new Asn1Boolean(await DecodeBoolean(inRenamed, length, cancellationToken).ConfigureAwait(false))); case Asn1Integer.Tag: return(new Asn1Integer(await DecodeNumeric(inRenamed, length, cancellationToken).ConfigureAwait(false))); case Asn1OctetString.Tag: return(new Asn1OctetString(length > 0 ? await DecodeOctetString(inRenamed, length, cancellationToken).ConfigureAwait(false) : new byte[0])); case Asn1Enumerated.Tag: return(new Asn1Enumerated(await DecodeNumeric(inRenamed, length, cancellationToken).ConfigureAwait(false))); 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((Asn1Identifier)_asn1Id.Clone(), new Asn1OctetString(length > 0 ? await DecodeOctetString(inRenamed, length, cancellationToken).ConfigureAwait(false) : new byte[0]))); }