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 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 #3
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 #4
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 #5
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);
        }
        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();
            }
        }
Exemple #7
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);
            }
        }
Exemple #8
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 #9
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);
        }