Exemple #1
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);
            }
        }
Exemple #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));
                }
            }
        }
Exemple #3
0
 public static int IntTrailingZerosCnt(int w)
 {
     return(0x20 -
            (((w & 0xffff) != 0)
                                         ? (((w & 0xff) != 0) ? ((((w & 15) != 0) ? (((w & 3) != 0) ? (((w & 1) != 0) ? 8 : 7) : (((w & 4) != 0) ? 6 : 5)) : (((w & 0x30) != 0) ? (((w & 0x10) != 0) ? 4 : 3) : (((w & 0x40) != 0) ? 2 : (((w & 0x80) != 0) ? 1 : 0)))) + 0x18) : (((((w = Asn1Util.UrShift(w, 8)) & 15) != 0) ? (((w & 3) != 0) ? (((w & 1) != 0) ? 8 : 7) : (((w & 4) != 0) ? 6 : 5)) : (((w & 0x30) != 0) ? (((w & 0x10) != 0) ? 4 : 3) : (((w & 0x40) != 0) ? 2 : (((w & 0x80) != 0) ? 1 : 0)))) + 0x10))
                                         : ((((w = Asn1Util.UrShift(w, 0x10)) & 0xff) != 0) ? ((((w & 15) != 0) ? (((w & 3) != 0) ? (((w & 1) != 0) ? 8 : 7) : (((w & 4) != 0) ? 6 : 5)) : (((w & 0x30) != 0) ? (((w & 0x10) != 0) ? 4 : 3) : (((w & 0x40) != 0) ? 2 : (((w & 0x80) != 0) ? 1 : 0)))) + 8) : ((((w = Asn1Util.UrShift(w, 8)) & 15) != 0) ? (((w & 3) != 0) ? (((w & 1) != 0) ? 8 : 7) : (((w & 4) != 0) ? 6 : 5)) : (((w & 0x30) != 0) ? (((w & 0x10) != 0) ? 4 : 3) : (((w & 0x40) != 0) ? 2 : (((w & 0x80) != 0) ? 1 : 0)))))));
 }
Exemple #4
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;
        }
Exemple #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);
        }
Exemple #6
0
        public virtual string ToHexString()
        {
            var str = new StringBuilder("").ToString();

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

            return(str);
        }
Exemple #7
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);
        }
Exemple #8
0
        public static int GetUlongBytesCount(long value)
        {
            var number = -72057594037927936L;
            var num2   = 8;

            while ((num2 > 1) && ((value & number) == 0L))
            {
                number = Asn1Util.UrShift(number, 8);
                num2--;
            }

            return(num2);
        }
Exemple #9
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);
        }
        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);
            }
        }
Exemple #11
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);
            }
        }
        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();
            }
        }
        public override void Encode(Asn1BerOutputStream outs, bool explicitTagging)
        {
            try
            {
                var bytes = Encoding.UTF8.GetBytes(Value);

                if (explicitTagging)
                {
                    outs.EncodeTag(Tag);
                }

                outs.EncodeLength(bytes.Length);
                outs.Write(bytes);
            }
            catch (IOException exception)
            {
                Console.Out.WriteLine("This JVM does not support UTF-8 encoding");
                Asn1Util.WriteStackTrace(exception, Console.Error);
            }
        }
Exemple #14
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);
            }
        }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            try
            {
                var bytes = Encoding.UTF8.GetBytes(Value);
                len = bytes.Length;
                buffer.Copy(bytes);
            }
            catch (IOException exception)
            {
                Console.Out.WriteLine("This JVM does not support UTF-8 encoding");
                Asn1Util.WriteStackTrace(exception, Console.Error);
            }

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

            return(len);
        }
Exemple #16
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);
        }
Exemple #17
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);
            }
        }
Exemple #18
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;
            }
        }
Exemple #19
0
        public void Init(string val, int radix)
        {
            var str = "";

            if (val[0] == '-')
            {
                val = val.Substring(1);
                str = "-";
            }

            if (val.StartsWith("0x"))
            {
                radix = 0x10;
                val   = val.Substring(2);
            }
            else if (val.StartsWith("0b"))
            {
                radix = 2;
                val   = val.Substring(2);
            }
            else if (val.StartsWith("0o"))
            {
                radix = 8;
                val   = val.Substring(2);
            }

            val = str + val;
            var startIndex = 0;
            var length     = val.Length;

            if (((radix != 2) && (radix != 0x10)) && ((radix != 10) && (radix != 8)))
            {
                throw new FormatException(Resources.Asn1InvalidFormatForBigIntegerValue);
            }

            if (val.Length == 0)
            {
                throw new FormatException(Resources.Asn1ZeroLengthBigInteger);
            }

            _sign = 1;

            var index = val.IndexOf('-');

            if (index != -1)
            {
                if (index != 0)
                {
                    throw new FormatException(Resources.Asn1IllegalEmbeddedMinusSign);
                }

                if (val.Length == 1)
                {
                    throw new FormatException(Resources.Asn1ZeroLengthBigInteger);
                }

                _sign = -1;

                startIndex = 1;
            }

            while ((startIndex < length) && (val[startIndex] == '0'))
            {
                startIndex++;
            }

            if (startIndex == length)
            {
                _sign  = 0;
                _value = Zero;
            }
            else
            {
                var num2 = length - startIndex;
                var num5 = Asn1Util.UrShift(num2 * BitsPerDigit[radix], 10) + 1;
                var num1 = (num5 + 0x1f) / 0x20;

                _value = new byte[num2];

                var num6 = num2 % DigitsPerByte[radix];

                if (num6 == 0)
                {
                    num6 = DigitsPerByte[radix];
                }

                var str2 = val.Substring(startIndex, num6);
                startIndex += num6;

                _value[_value.Length - 1] = Convert.ToByte(str2, radix);

                if (_value[_value.Length - 1] < 0)
                {
                    throw new FormatException(Resources.Asn1IllegalDigit);
                }

                var  y = ByteRadix[radix];
                byte z;

                while (startIndex < val.Length)
                {
                    str2        = val.Substring(startIndex, DigitsPerByte[radix]);
                    startIndex += DigitsPerByte[radix];
                    z           = Convert.ToByte(str2, radix);

                    if (z < 0)
                    {
                        throw new FormatException(Resources.Asn1IllegalDigit);
                    }

                    DestructiveMulAdd(_value, y, z);
                }

                _value = TrustedStripLeadingZeroInts(_value);
            }
        }