/// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     //Ref. X.680: 8.6.2.2
     buffer.WriteBytes(ByteArrayValue);
     buffer.WriteByte(unusedBitsInLastByte);
     return(ByteArrayValue.Length + 1);
 }
        /// <summary>
        /// Encodes the data of this object to the buffer.
        /// </summary>
        /// <param name="buffer">A buffer to which the encoding result will be written.</param>
        /// <returns>The length of the encoding result of the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of encoding Value.
        /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
        /// </remarks>
        protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
        {
            var res = EncodeIdentifier(Value);

            buffer.WriteBytes(res);
            return(res.Length);
        }
示例#3
0
        //Constraint is not needed for primitive type UniversalString.
        //Therefore no need to override VerifyConstraints method.

        #region BER

        /// <summary>
        /// Encodes the data of this object to the buffer.
        /// </summary>
        /// <param name="buffer">A buffer to which the encoding result will be written.</param>
        /// <returns>The length of the encoding result of the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of encoding Value.
        /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
        /// </remarks>
        protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
        {
            if (this.Value == null)
            {
                throw new Asn1EmptyDataException(ExceptionMessages.EmptyData);
            }
            byte[] result = Encoding.BigEndianUnicode.GetBytes(this.Value);
            buffer.WriteBytes(result);
            return(result.Length);
        }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     if (this.Value == null)
     {
         throw new Asn1EmptyDataException(ExceptionMessages.EmptyData);
     }
     byte[] result = UniversalStringEncode(this.Value);
     buffer.WriteBytes(result);
     return result.Length;
 }
        /// <summary>
        /// Encodes a length to the buffer.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="length">The length to be written to the buffer.</param>
        /// <returns>The length of "the encoded length's encoding result".</returns>
        public static int LengthBerEncode(IAsn1BerEncodingBuffer buffer, int length)
        {
            if (length < 0)
            {
                throw new Asn1InvalidArgument(ExceptionMessages.LengthNegative);
            }
            if (length < 128)
            {
                buffer.WriteByte((byte)length);
                return(1);
            }
            else
            {
                //The encoding result of a length that no less than 128 will be stored in at least two bytes.
                //The first byte:  1xxxxxxx, xxxxxxx is the length of the following encoding result.
                //The next bytes:  the length's encoding result, big endian.
                //Ref. X.690: 8.1.3
                byte[] bytes = BitConverter.GetBytes(length);
                //ensure bytes is big endian
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(bytes);
                }

                //delete the redundant zeros
                int zeroNum = 0;
                for (int i = 0; i < bytes.Length; i++)
                {
                    if (bytes[i] == 0)
                    {
                        zeroNum++;
                    }
                    else
                    {
                        break;
                    }
                }
                //zeroNum won't be equal to bytes.Length since length>=128.

                //Encode data first, then encode length of the data's encoding result since buffer is reversed.
                buffer.WriteBytes(bytes, zeroNum, bytes.Length - zeroNum);
                buffer.WriteByte((byte)((bytes.Length - zeroNum) | (1 << 7)));//set the first bit to 1
                return(1 + bytes.Length - zeroNum);
            }
        }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     byte[] result = IntegerEncoding(Value);
     buffer.WriteBytes(result);
     return result.Length;
 }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     byte[] res = EncodeIdentifier(this.Value);
     buffer.WriteBytes(res);
     return res.Length;
 }
示例#8
0
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     byte[] result = IntegerEncoding(Value);
     buffer.WriteBytes(result);
     return(result.Length);
 }
        /// <summary>
        /// Encodes a length to the buffer.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="length">The length to be written to the buffer.</param>
        /// <returns>The length of "the encoded length's encoding result".</returns>
        public static int LengthBerEncode(IAsn1BerEncodingBuffer buffer, int length)
        {
            if (length < 0)
            {
                throw new Asn1InvalidArgument(ExceptionMessages.LengthNegative);
            }
            if (length < 128)
            {
                buffer.WriteByte((byte)length);
                return 1;
            }
            else
            {
                //The encoding result of a length that no less than 128 will be stored in at least two bytes.
                //The first byte:  1xxxxxxx, xxxxxxx is the length of the following encoding result.
                //The next bytes:  the length's encoding result, big endian.
                //Ref. X.690: 8.1.3
                byte[] bytes = BitConverter.GetBytes(length);
                //ensure bytes is big endian
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(bytes);
                }

                //delete the redundant zeros
                int zeroNum = 0;
                for (int i = 0; i < bytes.Length; i++)
                {
                    if (bytes[i] == 0)
                    {
                        zeroNum++;
                    }
                    else
                    {
                        break;
                    }
                }
                //zeroNum won't be equal to bytes.Length since length>=128.

                //Encode data first, then encode length of the data's encoding result since buffer is reversed.
                buffer.WriteBytes(bytes, zeroNum, bytes.Length - zeroNum);
                buffer.WriteByte((byte)((bytes.Length - zeroNum) | (1 << 7)));//set the first bit to 1
                return 1 + bytes.Length - zeroNum;
            }
        }
示例#10
0
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     buffer.WriteBytes(ByteArrayValue);
     return(ByteArrayValue == null ? 0 : ByteArrayValue.Length);
 }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     //Ref. X.680: 8.6.2.2
     buffer.WriteBytes(ByteArrayValue);
     buffer.WriteByte(unusedBitsInLastByte);
     return ByteArrayValue.Length + 1;
 }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     buffer.WriteBytes(ByteArrayValue);
     return ByteArrayValue == null ? 0 : ByteArrayValue.Length;
 }