コード例 #1
0
        public static string DecodeAsciiString(IBerInput input, int length)
        {
            var intLength = (int)length;
            var bytes     = ReadString(input, ref intLength);

            return(AsciiEncoding.GetString(bytes, 0, intLength));
        }
コード例 #2
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        public static DateTime DecodeGeneralizedTime(IBerInput input, int length)
        {
            var str = DecodeAsciiString(input, length);
             int year, month, day, hour, minute, second, millisecond;
             var isUtc = false;

             try
             {
            year = int.Parse(str.Substring(0, 4));
            month = int.Parse(str.Substring(4, 2));
            day = int.Parse(str.Substring(6, 2));
            hour = int.Parse(str.Substring(8, 2));
            minute = int.Parse(str.Substring(10, 2));
            second = int.Parse(str.Substring(12, 2));

            var suffix = str.Substring(15);
            if(suffix.EndsWith("Z")) // UTC
            {
               suffix = suffix.Substring(0, suffix.Length - 1);
               isUtc = true;
            }

            millisecond = int.Parse(suffix);

            return new DateTime(year, month, day, hour, minute, second, millisecond, isUtc ? DateTimeKind.Utc : DateTimeKind.Local);
             }
             catch(Exception ex)
             {
            Debug.WriteLine(ex);

            return DateTime.MinValue;
             }
        }
コード例 #3
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        public static string DecodeAsciiString(IBerInput input, int length)
        {
            var intLength = (int)length;
             var bytes = ReadString(input, ref intLength);

             return AsciiEncoding.GetString(bytes, 0, intLength);
        }
コード例 #4
0
        public static int DecodeLength(IBerInput input)
        {
            int value;
            int byteCount;

            value = input.ReadByte();

            if ((value & 0x80) != 0)
            {
                byteCount = value & 0x7F;

                if (byteCount == 0)
                {
                    value = BerDefinitions.IndefiniteLength; // indefinite length form
                }
                else
                {
                    value = 0;

                    for ( ; byteCount > 0; byteCount--)
                    {
                        value = (value << 8) | input.ReadByte();
                    }
                }
            }

            return(value);
        }
コード例 #5
0
        public static double DecodeReal(IBerInput input, int length)
        {
            double value = 0;

            if (length != 0)
            {
                byte preamble = input.ReadByte();

                if (length == 1 && preamble == 0x40) // positive infinity
                {
                    value = double.PositiveInfinity;
                }
                else if (length == 1 && preamble == 0x41) // negative infinity
                {
                    value = double.NegativeInfinity;
                }
                else if (length == 1 && preamble == 0x42) // not a number
                {
                    value = double.NaN;
                }
                else
                {
                    long longValue;

                    int exponentLength = 1 + (preamble & 3);
                    int sign           = preamble & 0x40;
                    int ff             = (preamble >> 2) & 3;

                    // Unpack mantissa & decrement exponent for base 2
                    long exponent = DecodeLong(input, exponentLength);
                    long mantissa = DecodeLong(input, length - exponentLength - 1, false) << ff;

                    // de-normalize mantissa (required by CER and DER)
                    while ((mantissa & 0x7FFFF00000000000L) == 0x0)
                    {
                        mantissa <<= 8;
                    }

                    while ((mantissa & 0x7FF0000000000000L) == 0x0)
                    {
                        mantissa <<= 1;
                    }

                    mantissa &= 0x0FFFFFFFFFFFFFL;

                    longValue  = (exponent + 1023) << 52;
                    longValue |= mantissa;

                    if (sign != 0)
                    {
                        ulong qword = (ulong)longValue | 0x8000000000000000UL;
                        longValue = (long)qword;
                    }

                    value = Int64BitsToDouble((long)longValue);
                }
            }

            return(value);
        }
コード例 #6
0
        public static int[] DecodeObjectIdentifier(IBerInput input, int length)
        {
            var  values        = new List <int>();
            int  subidentifier = 0;
            uint bytesRead     = 0;

            while (bytesRead++ < length)
            {
                byte uByte = input.ReadByte();

                subidentifier = ((subidentifier << 7) | (uByte & 0x7F));

                if ((uByte & 0x80) == 0)
                {
                    if (values.Count > 0)
                    {
                        values.Add(subidentifier);
                    }
                    else
                    {
                        values.Add(subidentifier / 40);
                        values.Add(subidentifier % 40);
                    }

                    subidentifier = 0;
                }
            }

            return(values.ToArray());
        }
コード例 #7
0
        public static DateTime DecodeGeneralizedTime(IBerInput input, int length)
        {
            var str = DecodeAsciiString(input, length);
            int year, month, day, hour, minute, second, millisecond;
            var isUtc = false;

            try
            {
                year   = int.Parse(str.Substring(0, 4));
                month  = int.Parse(str.Substring(4, 2));
                day    = int.Parse(str.Substring(6, 2));
                hour   = int.Parse(str.Substring(8, 2));
                minute = int.Parse(str.Substring(10, 2));
                second = int.Parse(str.Substring(12, 2));

                var suffix = str.Substring(15);
                if (suffix.EndsWith("Z")) // UTC
                {
                    suffix = suffix.Substring(0, suffix.Length - 1);
                    isUtc  = true;
                }

                millisecond = int.Parse(suffix);

                return(new DateTime(year, month, day, hour, minute, second, millisecond, isUtc ? DateTimeKind.Utc : DateTimeKind.Local));
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Exception: BerLib/BerEncoding/DecodeGeneralizedTime: {ex.Message}");
                return(DateTime.MinValue);
            }
        }
コード例 #8
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        public static byte[] DecodeByteArray(IBerInput input, int length)
        {
            var bytes = new byte[length];

             for(int index = 0; index < length; index++)
            bytes[index] = input.ReadByte();

             return bytes;
        }
コード例 #9
0
        public static byte[] DecodeByteArray(IBerInput input, int length)
        {
            var bytes = new byte[length];

            for (int index = 0; index < length; index++)
            {
                bytes[index] = input.ReadByte();
            }

            return(bytes);
        }
コード例 #10
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));
        }
コード例 #11
0
        // ====================================================================
        //
        // Decode functions
        //
        // ====================================================================
        #region DecodeFunctions
        public static BerTag DecodeTag(IBerInput input)
        {
            var tagByte = input.ReadByte();
            var tag     = new BerTag((byte)(tagByte & 0xE0), (uint)(tagByte & 0x1F));

            if (tag.Number == 0x1F)
            {
                tag.Number = DecodeMultiByteInteger(input);
            }

            return(tag);
        }
コード例 #12
0
ファイル: EmberReader.cs プロジェクト: jv42/ember-plus
        /// <summary>
        /// Creates a nested reader from another EmberReader instance.
        /// The passed <paramref name="parentReader"/> must be positioned
        /// on a container.
        /// The newly created nested reader will signal Eof when all bytes
        /// of this container have been read.
        /// </summary>
        /// <param name="parentReader">The base reader.</param>
        public EmberReader(EmberReader parentReader)
        {
            if(parentReader == null)
            throw new ArgumentNullException("parentReader");

             if(parentReader.IsContainer == false)
            throw new ArgumentException("parentReader is not positioned on a container");

             _input = parentReader._input;
             _parentReader = parentReader;

             _bytesAvailable = parentReader.Length;
        }
コード例 #13
0
        public static ulong DecodeMultiByteLong(IBerInput input)
        {
            byte  currentByte;
            ulong value = 0;

            do
            {
                currentByte = input.ReadByte();
                value       = (value << 7) | (byte)(currentByte & ~0x80);
            } while((currentByte & 0x80) != 0);

            return(value);
        }
コード例 #14
0
        public static int[] DecodeRelativeOid(IBerInput input, int length)
        {
            var subidentifiers = new List <int>();

            while (length > 0)
            {
                int count;
                subidentifiers.Add((int)DecodeMultiByteInteger(input, out count));

                length -= count;
            }

            return(subidentifiers.ToArray());
        }
コード例 #15
0
        public static uint DecodeMultiByteInteger(IBerInput input, out int consumedByteCount)
        {
            byte currentByte;
            uint value = 0;
            var  count = 0;

            do
            {
                currentByte = input.ReadByte();
                value       = (value << 7) | (byte)(currentByte & ~0x80);
                count++;
            } while((currentByte & 0x80) != 0);

            consumedByteCount = count;
            return(value);
        }
コード例 #16
0
        public static int DecodeInteger(IBerInput input, int length)
        {
            int value = 0;
            int readByte;

            for (uint byteCount = 0; byteCount < length; byteCount++)
            {
                readByte = input.ReadByte();

                if (byteCount == 0 && (readByte & 0x80) != 0)
                {
                    readByte -= 0x100;
                }

                value = (value << 8) | readByte;
            }

            return(value);
        }
コード例 #17
0
        static byte[] ReadString(IBerInput input, ref int length)
        {
            var bytes = new byte[length];

            if (length > 0)
            {
                for (int nIndex = 0; nIndex < length; nIndex++)
                {
                    bytes[nIndex] = input.ReadByte();
                }

                // strip trailing zeroes
                while (length > 0 && bytes[length - 1] == 0)
                {
                    length--;
                }
            }

            return(bytes);
        }
コード例 #18
0
        static long DecodeLong(IBerInput input, int length, bool isSigned)
        {
            long value = 0;
            long readByte;

            for (uint byteCount = 0; byteCount < length; byteCount++)
            {
                readByte = input.ReadByte();

                if (byteCount == 0 &&
                    (readByte & 0x80) != 0 &&
                    isSigned)
                {
                    readByte -= 0x100;
                }

                value = (value << 8) | readByte;
            }

            return(value);
        }
コード例 #19
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
 public static long DecodeLong(IBerInput input, int length)
 {
     return DecodeLong(input, length, true);
 }
コード例 #20
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        public static int DecodeLength(IBerInput input)
        {
            int value;
             int byteCount;

             value = input.ReadByte();

             if((value & 0x80) != 0)
             {
            byteCount = value & 0x7F;

            if(byteCount == 0)
            {
               value = BerDefinitions.IndefiniteLength; // indefinite length form
            }
            else
            {
               value = 0;

               for( ; byteCount > 0; byteCount--)
                  value = (value << 8) | input.ReadByte();
            }
             }

             return value;
        }
コード例 #21
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        public static uint DecodeMultiByteInteger(IBerInput input)
        {
            int count;

             return DecodeMultiByteInteger(input, out count);
        }
コード例 #22
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        public static uint DecodeMultiByteInteger(IBerInput input, out int consumedByteCount)
        {
            byte currentByte;
             uint value = 0;
             var count = 0;

             do
             {
            currentByte = input.ReadByte();
            value = (value << 7) | (byte)(currentByte & ~0x80);
            count++;

             } while((currentByte & 0x80) != 0);

             consumedByteCount = count;
             return value;
        }
コード例 #23
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        static long DecodeLong(IBerInput input, int length, bool isSigned)
        {
            long value = 0;
             long readByte;

             for(uint byteCount = 0; byteCount < length; byteCount++)
             {
            readByte = input.ReadByte();

            if(byteCount == 0
            && (readByte & 0x80) != 0
            && isSigned)
               readByte -= 0x100;

            value = (value << 8) | readByte;
             }

             return value;
        }
コード例 #24
0
 public static long DecodeLong(IBerInput input, int length)
 {
     return(DecodeLong(input, length, true));
 }
コード例 #25
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        public static int[] DecodeObjectIdentifier(IBerInput input, int length)
        {
            var values = new List<int>();
             int subidentifier = 0;
             uint bytesRead = 0;

             while(bytesRead++ < length)
             {
            byte uByte = input.ReadByte();

            subidentifier = ((subidentifier << 7) | (uByte & 0x7F));

            if((uByte & 0x80) == 0)
            {
               if(values.Count > 0)
               {
                  values.Add(subidentifier);
               }
               else
               {
                  values.Add(subidentifier / 40);
                  values.Add(subidentifier % 40);
               }

               subidentifier = 0;
            }
             }

             return values.ToArray();
        }
コード例 #26
0
ファイル: BerFormatter.cs プロジェクト: jv42/ember-plus
        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);
        }
コード例 #27
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        public static double DecodeReal(IBerInput input, int length)
        {
            double value = 0;

             if(length != 0)
             {
            byte preamble = input.ReadByte();

            if(length == 1 && preamble == 0x40) // positive infinity
            {
               value = double.PositiveInfinity;
            }
            else if(length == 1 && preamble == 0x41) // negative infinity
            {
               value = double.NegativeInfinity;
            }
            else
            {
               long longValue;

               int exponentLength = 1 + (preamble & 3);
               int sign = preamble & 0x40;
               int ff = (preamble >> 2) & 3;

               // Unpack mantissa & decrement exponent for base 2
               long exponent = DecodeLong(input, exponentLength);
               long mantissa = DecodeLong(input, length - exponentLength - 1, false) << ff;

               // de-normalize mantissa (required by CER and DER)
               while((mantissa & 0x7FFFF00000000000L) == 0x0)
                  mantissa <<= 8;

               while((mantissa & 0x7FF0000000000000L) == 0x0)
                  mantissa <<= 1;

               mantissa &= 0x0FFFFFFFFFFFFFL;

               longValue = (exponent + 1023) << 52;
               longValue |= mantissa;

               if(sign != 0)
               {
                  ulong qword = (ulong)longValue | 0x8000000000000000UL;
                  longValue = (long)qword;
               }

               value = Int64BitsToDouble((long)longValue);
            }
             }

             return value;
        }
コード例 #28
0
        public static uint DecodeMultiByteInteger(IBerInput input)
        {
            int count;

            return(DecodeMultiByteInteger(input, out count));
        }
コード例 #29
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        public static int[] DecodeRelativeOid(IBerInput input, int length)
        {
            var subidentifiers = new List<int>();

             while(length > 0)
             {
            int count;
            subidentifiers.Add((int)DecodeMultiByteInteger(input, out count));

            length -= count;
             }

             return subidentifiers.ToArray();
        }
コード例 #30
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        // ====================================================================
        //
        // Decode functions
        //
        // ====================================================================
        public static BerTag DecodeTag(IBerInput input)
        {
            var tagByte = input.ReadByte();
             var tag = new BerTag((byte)(tagByte & 0xE0), (uint)(tagByte & 0x1F));

             if(tag.Number == 0x1F)
            tag.Number = DecodeMultiByteInteger(input);

             return tag;
        }
コード例 #31
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        static byte[] ReadString(IBerInput input, ref int length)
        {
            var bytes = new byte[length];

             if(length > 0)
             {
            for(int nIndex = 0; nIndex < length; nIndex++)
               bytes[nIndex] = input.ReadByte();

            // strip trailing zeroes
            while(length > 0 && bytes[length - 1] == 0)
               length--;
             }

             return bytes;
        }
コード例 #32
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        public static int DecodeInteger(IBerInput input, int length)
        {
            int value = 0;
             int readByte;

             for(uint byteCount = 0; byteCount < length; byteCount++)
             {
            readByte = input.ReadByte();

            if(byteCount == 0 && (readByte & 0x80) != 0)
               readByte -= 0x100;

            value = (value << 8) | readByte;
             }

             return value;
        }
コード例 #33
0
 public static bool DecodeBoolean(IBerInput input)
 {
     return(input.ReadByte() != 0);
 }
コード例 #34
0
        /// <summary>
        /// Creates a new EmberReader instance configured to read BER encoded data
        /// from <paramref name="input"/>.
        /// </summary>
        /// <param name="input">The input to read BER encoded data from.</param>
        public EmberReader(IBerInput input)
        {
            Debug.Assert(input != null);

            _input = input;
        }
コード例 #35
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
        public static ulong DecodeMultiByteLong(IBerInput input)
        {
            byte currentByte;
             ulong value = 0;

             do
             {
            currentByte = input.ReadByte();
            value = (value << 7) | (byte)(currentByte & ~0x80);

             } while((currentByte & 0x80) != 0);

             return value;
        }
コード例 #36
0
ファイル: EmberReader.cs プロジェクト: jv42/ember-plus
        /// <summary>
        /// Creates a new EmberReader instance configured to read BER encoded data
        /// from <paramref name="input"/>.
        /// </summary>
        /// <param name="input">The input to read BER encoded data from.</param>
        public EmberReader(IBerInput input)
        {
            Debug.Assert(input != null);

             _input = input;
        }
コード例 #37
0
ファイル: BerEncoding.cs プロジェクト: jv42/ember-plus
 public static bool DecodeBoolean(IBerInput input)
 {
     return input.ReadByte() != 0;
 }