예제 #1
0
        static int DecodeHeader(IBerInput input, BerTag expectedTag)
        {
            var tag = BerEncoding.DecodeTag(input);

            if (tag != expectedTag)
            {
                throw new BerException(4001, String.Format("Expected tag {0}, found tag {1}", tag, expectedTag));
            }

            return(BerEncoding.DecodeLength(input));
        }
예제 #2
0
        /// <summary>
        /// Decodes the value of the current TLV as an RelativeOid.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>An array of integers containing the subidentifiers of the RelativeOid value of the current TLV.</returns>
        public int[] GetRelativeOid()
        {
            if (IsContainer)
            {
                ThrowError(207, "Invalid RelativeOid encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.RelativeOid || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeRelativeOid(input, Length));
        }
예제 #3
0
        /// <summary>
        /// Decodes the value of the current TLV as an GeneralizedTime represented
        /// as a DateTime structure.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>DateTime value containing the GeneralizedTime value of the current TLV.</returns>
        public DateTime GetGeneralizedTime()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(208, "Invalid GeneralizedTime encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.GeneralizedTime || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeGeneralizedTime(input, Length));
        }
예제 #4
0
        /// <summary>
        /// Decodes the value of the current TLV as an OBJECT IDENTIFIER represented
        /// as an integer array, each integer containing one sub-identifier.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>Integer array containing the OBJECT IDENTIFIER value of the current TLV.</returns>
        public int[] GetObjectIdentifier()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(209, "Invalid ObjectIdentifier encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.ObjectIdentifier || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeObjectIdentifier(input, Length));
        }
예제 #5
0
        /// <summary>
        /// Decodes the value of the current TLV as an octet string (byte array).
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>Byte array containing the OCTET STRING value of the current TLV.</returns>
        public byte[] GetOctetString()
        {
            if (IsContainer)
            {
                ThrowError(206, "Invalid OctetString encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.OctetString || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeByteArray(input, Length));
        }
예제 #6
0
        /// <summary>
        /// Decodes the value of the current TLV an UTF-8 string.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The string value of the current TLV.</returns>
        public string GetString()
        {
            if (IsContainer)
            {
                ThrowError(205, "Invalid String encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.UTF8String || Type == BerType.Bitstring || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeUtf8String(input, Length));
        }
예제 #7
0
        /// <summary>
        /// Decodes the value of the current TLV a 64bit integer.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The integer value of the current TLV.</returns>
        public long GetLong()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(203, "Invalid Integer encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.Integer || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeLong(input, Length));
        }
예제 #8
0
        /// <summary>
        /// Decodes the value of the current TLV as boolean.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The boolean value of the current TLV.</returns>
        public bool GetBoolean()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(201, "Invalid Boolean encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.Boolean || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeBoolean(input));
        }
예제 #9
0
        public static byte[] Encode(string format, params object[] args)
        {
            var output     = new BerMemoryOutput();
            var paramIndex = 0;

            for (int charIndex = 0; charIndex < format.Length; charIndex++)
            {
                var ch = format[charIndex];

                switch (ch)
                {
                case '{':
                    EncodeHeader(output, TypeTags.Sequence, BerDefinitions.IndefiniteLength);
                    break;

                case '}':
                    output.WriteByte(0);
                    output.WriteByte(0);
                    break;

                case 'b':
                {
                    EncodeHeader(output, TypeTags.Boolean, 1);
                    BerEncoding.EncodeBoolean(output, (bool)args[paramIndex++]);
                    break;
                }

                case 'i':
                case 'd':
                {
                    var value       = (int)args[paramIndex++];
                    var valueLength = BerEncoding.GetIntegerLength(value);

                    EncodeHeader(output, TypeTags.Integer, valueLength);
                    BerEncoding.EncodeInteger(output, value, (int)valueLength);
                    break;
                }

                case 'l':
                case 't':
                {
                    var value       = (long)args[paramIndex++];
                    var valueLength = BerEncoding.GetLongLength(value);

                    EncodeHeader(output, TypeTags.Integer, valueLength);
                    BerEncoding.EncodeLong(output, value, (int)valueLength);
                    break;
                }

                case 's':
                {
                    var value       = (string)args[paramIndex++];
                    var valueLength = BerEncoding.GetUtf8StringLength(value);

                    EncodeHeader(output, TypeTags.UTF8String, valueLength);
                    BerEncoding.EncodeUtf8String(output, value);
                    break;
                }

                case 'y':
                {
                    var value       = (byte[])args[paramIndex++];
                    var valueLength = value.Length;

                    EncodeHeader(output, TypeTags.OctetString, valueLength);
                    BerEncoding.EncodeByteArray(output, value);
                    break;
                }

                case 'f':
                case 'r':
                {
                    var value       = (double)args[paramIndex++];
                    var local       = new BerMemoryOutput();
                    var valueLength = BerEncoding.EncodeReal(local, value);
                    EncodeHeader(output, TypeTags.Real, valueLength);
                    output.WriteBytes(local.Memory);
                    break;
                }

                case 'o':
                {
                    var value       = (int[])args[paramIndex++];
                    var local       = new BerMemoryOutput();
                    var valueLength = BerEncoding.EncodeRelativeOid(local, value);
                    EncodeHeader(output, TypeTags.RelativeOid, valueLength);
                    output.WriteBytes(local.Memory);
                    break;
                }

                case 'g':
                {
                    var value       = (DateTime)args[paramIndex++];
                    var local       = new BerMemoryOutput();
                    var valueLength = BerEncoding.EncodeGeneralizedTime(local, value);
                    EncodeHeader(output, TypeTags.GeneralizedTime, valueLength);
                    output.WriteBytes(local.Memory);
                    break;
                }
                }
            }

            return(output.ToArray());
        }
예제 #10
0
 static void EncodeHeader(IBerOutput output, BerTag tag, int length)
 {
     BerEncoding.EncodeTag(output, tag);
     BerEncoding.EncodeLength(output, length);
 }
예제 #11
0
        public static object[] Decode(string format, IList <byte> data)
        {
            var input   = new BerMemoryInput(data);
            var objects = new List <object>();

            for (int charIndex = 0; charIndex < format.Length; charIndex++)
            {
                var ch = format[charIndex];

                switch (ch)
                {
                case '{':
                    DecodeHeader(input, TypeTags.Sequence);
                    break;

                case '}':
                {
                    if (input.ReadByte() != 0)
                    {
                        throw new BerException(4002, "Expected terminator");
                    }
                    if (input.ReadByte() != 0)
                    {
                        throw new BerException(4002, "Expected terminator");
                    }
                    break;
                }

                case 'b':
                {
                    DecodeHeader(input, TypeTags.Boolean);
                    objects.Add(BerEncoding.DecodeBoolean(input));
                    break;
                }

                case 'i':
                case 'd':
                {
                    var valueLength = DecodeHeader(input, TypeTags.Integer);
                    objects.Add(BerEncoding.DecodeInteger(input, valueLength));
                    break;
                }

                case 'l':
                case 't':
                {
                    var valueLength = DecodeHeader(input, TypeTags.Integer);
                    objects.Add(BerEncoding.DecodeLong(input, valueLength));
                    break;
                }

                case 's':
                {
                    var valueLength = DecodeHeader(input, TypeTags.UTF8String);
                    objects.Add(BerEncoding.DecodeUtf8String(input, valueLength));
                    break;
                }

                case 'y':
                {
                    var valueLength = DecodeHeader(input, TypeTags.OctetString);
                    objects.Add(BerEncoding.DecodeByteArray(input, valueLength));
                    break;
                }

                case 'f':
                case 'r':
                {
                    var valueLength = DecodeHeader(input, TypeTags.Real);
                    objects.Add(BerEncoding.DecodeReal(input, valueLength));
                    break;
                }

                case 'o':
                {
                    var valueLength = DecodeHeader(input, TypeTags.RelativeOid);
                    objects.Add(BerEncoding.DecodeRelativeOid(input, valueLength));
                    break;
                }

                case 'g':
                {
                    var valueLength = DecodeHeader(input, TypeTags.GeneralizedTime);
                    objects.Add(BerEncoding.DecodeGeneralizedTime(input, valueLength));
                    break;
                }
                }
            }

            return(objects.ToArray());
        }