Exemplo n.º 1
0
        public virtual void StartElement(Asn1Tag tag, int len, byte[] tagLenBytes)
        {
            PrintOffset();

            new StringBuilder(40);             // WTF?

            var index = 0;

            while (index < tagLenBytes.Length)
            {
                _printStream.Write(Asn1Util.ToHexString(tagLenBytes[index]));
                _printStream.Write(' ');
                index++;
            }

            while (index < MaxBytesPerLine)
            {
                _printStream.Write("   ");
                index++;
            }

            _printStream.Write(": ");
            _printStream.Write(tag.Constructed ? "C " : "P ");
            _printStream.Write(tag + " ");
            _printStream.WriteLine(Convert.ToString(len));
            _offset += tagLenBytes.Length;
        }
Exemplo n.º 2
0
        public virtual void EncodeUnivString(int[] data, bool explicitTagging, Asn1Tag tag)
        {
            if (explicitTagging)
            {
                EncodeTag(tag);
            }
            if (data == null)
            {
                EncodeLength(0);
            }
            else
            {
                EncodeLength(data.Length * 4);
                var length = data.Length;

                for (var i = 0; i < length; ++i)
                {
                    var number = data[i];
                    OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 0x18) & 0xff));
                    OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 0x10) & 0xff));
                    OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 8) & 0xff));
                    OutputStream.WriteByte((byte)(number & 0xff));
                }
            }
        }
Exemplo n.º 3
0
        public virtual void EncodeIdentifier(long ident)
        {
            var number          = 0x7fL;
            var identBytesCount = Asn1RunTime.GetIdentBytesCount(ident);

            number = number << (7 * identBytesCount);

            if (identBytesCount > 0)
            {
                while (identBytesCount > 0)
                {
                    number = Asn1Util.UrShift(number, 7);
                    identBytesCount--;

                    var num3 = Asn1Util.UrShift(ident & number, identBytesCount * 7);

                    if (identBytesCount != 0)
                    {
                        num3 |= 0x80L;
                    }

                    OutputStream.WriteByte((byte)num3);
                }
            }
            else
            {
                OutputStream.WriteByte(0);
            }
        }
Exemplo n.º 4
0
        public virtual string ToHexString()
        {
            var str = new StringBuilder("").ToString();

            foreach (var b in Value)
            {
                str = str + Asn1Util.ToHexString(b);
            }

            return(str);
        }
Exemplo n.º 5
0
        private static int TrailingZerosCnt(long w)
        {
            var num = Asn1RunTime.IntTrailingZerosCnt((int)w);

            if (num >= RealBase16)
            {
                return(Asn1RunTime.IntTrailingZerosCnt((int)Asn1Util.UrShift(w, RealBase16)) + RealBase16);
            }

            return(num);
        }
Exemplo n.º 6
0
        public override string ToString()
        {
            var num  = ByteIndex + 1;
            var num2 = Data.Length - num;
            var str  = new StringBuilder("").ToString();

            for (var i = 0; i < num2; ++i)
            {
                str = str + Asn1Util.ToHexString(Data[i + num]);
            }

            return(str);
        }
Exemplo n.º 7
0
        public override string ToString()
        {
            var str = new StringBuilder("").ToString();

            if (Value != null)
            {
                foreach (var b in Value)
                {
                    str = str + Asn1Util.ToHexString(b);
                }
            }

            return(str);
        }
Exemplo n.º 8
0
        public virtual void EncodeCharString(string data, bool explicitTagging, Asn1Tag tag)
        {
            if (explicitTagging)
            {
                EncodeTag(tag);
            }

            if (data == null)
            {
                EncodeLength(0);
            }
            else
            {
                EncodeLength(data.Length);
                var buffer = Asn1Util.ToByteArray(data);
                OutputStream.Write(buffer, 0, buffer.Length);
            }
        }
Exemplo n.º 9
0
        public override void EncodeCharString(string value, bool explicitTagging, Asn1Tag tag)
        {
            if ((value == null) || (value.Length <= 0x3e8))
            {
                base.EncodeCharString(value, explicitTagging, tag);
            }
            else
            {
                var data = Asn1Util.ToByteArray(value);

                if (explicitTagging)
                {
                    EncodeTag(tag.Class, 0x20, tag.IdCode);
                }

                EncodeOctetString(data, false, tag);
            }
        }
Exemplo n.º 10
0
        public override void EncodeUnivString(int[] value, bool explicitTagging, Asn1Tag tag)
        {
            if ((value == null) || (value.Length <= 250))
            {
                base.EncodeUnivString(value, explicitTagging, tag);
            }
            else
            {
                if (explicitTagging)
                {
                    EncodeTagAndIndefLen(Asn1UniversalString.Tag.Class, 0x20, Asn1UniversalString.Tag.IdCode);
                }
                else
                {
                    OutputStream.WriteByte(0x80);
                }

                for (var i = 0; i < value.Length; i += 250)
                {
                    var num2 = value.Length - i;

                    if (num2 > 250)
                    {
                        num2 = 250;
                    }

                    EncodeTagAndLength(Asn1OctetString.Tag, num2 * 4);

                    for (int j = 0; j < num2; j++)
                    {
                        var number = value[j + i];

                        OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 0x18) & 0xff));
                        OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 0x10) & 0xff));
                        OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 8) & 0xff));
                        OutputStream.WriteByte((byte)(number & 0xff));
                    }
                }

                EncodeEoc();
            }
        }
Exemplo n.º 11
0
        public virtual void EncodeLength(int len)
        {
            if (len >= 0)
            {
                var bytesCount = Asn1Util.GetBytesCount(len);

                if (len > 0x7f)
                {
                    OutputStream.WriteByte((byte)(bytesCount | 0x80));
                }
                for (var i = (8 * bytesCount) - 8; i >= 0; i -= 8)
                {
                    var num3 = (byte)((len >> i) & 0xff);
                    OutputStream.WriteByte(num3);
                }
            }
            else if (len == Asn1Status.IndefiniteLength)
            {
                OutputStream.WriteByte(0x80);
            }
        }
Exemplo n.º 12
0
        public override void Encode(Asn1BerOutputStream outs, bool explicitTagging)
        {
            if (explicitTagging)
            {
                outs.EncodeTag(Tag);
            }

            if (Value == 0.0)
            {
                outs.EncodeLength(0);
            }
            else if (Value == double.NegativeInfinity)
            {
                outs.EncodeIntValue(MinusInfinity, true);
            }
            else if (Value == double.PositiveInfinity)
            {
                outs.EncodeIntValue(PlusInfinity, true);
            }
            else
            {
                var len  = 1;
                var num2 = BitConverter.DoubleToInt64Bits(Value);
                var num3 = ((num2 >> RealIso6093Mask) == 0L) ? 1 : -1;
                var num4 = ((int)((num2 >> 0x34) & 0x7ffL)) - 0x433;
                var w    = (num4 == 0) ? ((num2 & 0xfffffffffffffL) << 1) : ((num2 & 0xfffffffffffffL) | 0x10000000000000L);

                if (w != 0L)
                {
                    var bits = TrailingZerosCnt(w);
                    w     = Asn1Util.UrShift(w, bits);
                    num4 += bits;
                    len  += Asn1Util.GetUlongBytesCount(w);
                }
                else
                {
                    len++;
                }

                var num7 = RealBinary;

                if (num3 == -1)
                {
                    num7 |= PlusInfinity;
                }

                var bytesCount = Asn1Util.GetBytesCount(num4);
                len += bytesCount;

                switch (bytesCount)
                {
                case RealExplen2:
                    break;

                case RealExplen3:
                    num7 |= 1;
                    break;

                case RealExplenLong:
                    num7 |= 2;
                    break;

                default:
                    num7 |= 3;
                    len++;
                    break;
                }

                outs.EncodeLength(len);
                outs.WriteByte((byte)num7);

                if ((num7 & 3) == 3)
                {
                    outs.EncodeIntValue(bytesCount, false);
                }

                outs.EncodeIntValue(num4, false);
                outs.EncodeIntValue(w, false);
            }
        }
Exemplo n.º 13
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            if (double.IsNegativeInfinity(Value))
            {
                len = buffer.EncodeIntValue(MinusInfinity);
            }
            else if (double.IsPositiveInfinity(Value))
            {
                len = buffer.EncodeIntValue(PlusInfinity);
            }

            else if (Value != 0.0)
            {
                var num2 = BitConverter.DoubleToInt64Bits(Value);
                var num3 = ((num2 >> RealIso6093Mask) == 0L) ? 1 : -1;
                var num4 = ((int)((num2 >> 0x34) & 0x7ffL)) - 0x433;
                var w    = (num4 == 0) ? ((num2 & 0xfffffffffffffL) << 1) : ((num2 & 0xfffffffffffffL) | 0x10000000000000L);

                if (w != 0L)
                {
                    var bits = TrailingZerosCnt(w);
                    w     = Asn1Util.UrShift(w, bits);
                    num4 += bits;
                }

                len += buffer.EncodeIntValue(w);

                var num7 = buffer.EncodeIntValue(num4);
                len += num7;

                var num8 = RealBinary;

                if (num3 == -1)
                {
                    num8 |= PlusInfinity;
                }

                switch (num7)
                {
                case RealExplen2:
                    break;

                case RealExplen3:
                    num8 |= 1;
                    break;

                case RealExplenLong:
                    num8 |= 2;
                    break;

                default:
                    num8 |= 3;
                    len  += buffer.EncodeIntValue(num7);
                    break;
                }

                buffer.Copy((byte)num8);
                len++;
            }

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Tag, len);
            }

            return(len);
        }
Exemplo n.º 14
0
        public virtual void Contents(byte[] data)
        {
            if (data.Length != 0)
            {
                PrintOffset();

                var flag     = true;
                var builder  = new StringBuilder(100);
                var builder2 = new StringBuilder(100);

                for (var i = 0; i < data.Length; ++i)
                {
                    builder.Append(Asn1Util.ToHexString(data[i]));
                    builder.Append(' ');

                    int num2 = data[i];

                    if ((num2 >= 0x20) && (num2 <= 0x7f))
                    {
                        builder2.Append((char)num2);
                    }
                    else
                    {
                        builder2.Append('.');
                    }

                    if (((i + 1) % MaxBytesPerLine) == 0)
                    {
                        if (!flag)
                        {
                            _printStream.Write("     : ");
                        }
                        else
                        {
                            flag = false;
                        }

                        _printStream.WriteLine(builder + ": " + builder2);

                        builder.Length  = 0;
                        builder2.Length = 0;
                    }
                }

                if (builder.Length > 0)
                {
                    while (builder.Length < 0x24)
                    {
                        builder.Append(' ');
                    }

                    if (!flag)
                    {
                        _printStream.Write("     : ");
                    }

                    _printStream.WriteLine(builder + ": " + builder2);
                }

                _offset += data.Length;
            }
        }