Ejemplo n.º 1
0
        public static int EncodeLength(IBerOutput output, int value)
        {
            int size = 1;

            if (value == BerDefinitions.IndefiniteLength)
            {
                output.WriteByte(0x80);
            }
            else
            {
                if (value <= 0x7F)
                {
                    output.WriteByte((byte)(value & 0x7F));
                }
                else
                {
                    int integerLength = GetIntegerLength((int)value);
                    output.WriteByte((byte)(0x80 | integerLength));

                    size += EncodeInteger(output, (int)value, integerLength);
                }
            }

            return(size);
        }
Ejemplo n.º 2
0
        public static int EncodeReal(IBerOutput output, double value)
        {
            var size = 0;

            if (double.IsPositiveInfinity(value))
            {
                output.WriteByte(0x40); // 01000000 Value is PLUS-INFINITY
                size = 1;
            }
            else if (double.IsNegativeInfinity(value)) // negative infinity
            {
                output.WriteByte(0x41);                // 01000001 Value is MINUS-INFINITY
                size = 1;
            }
            else if (double.IsNaN(value))
            {
                output.WriteByte(0x42); // 01000010 Value is NOT-A-NUMBER
                size = 1;
            }
            else
            {
                long longValue = DoubleToInt64Bits(value);

                if (longValue != 0)
                {
                    long exponent = ((0x7FF0000000000000L & longValue) >> 52) - 1023;
                    long mantissa = 0x000FFFFFFFFFFFFFL & longValue;
                    mantissa |= 0x0010000000000000L; // set virtual delimeter

                    // normalize mantissa (required by CER and DER)
                    while ((mantissa & 0xFF) == 0)
                    {
                        mantissa >>= 8;
                    }

                    while ((mantissa & 0x01) == 0)
                    {
                        mantissa >>= 1;
                    }

                    int exponentLength = GetLongLength(exponent);
                    Debug.Assert(exponentLength <= 3);

                    byte preamble = 0x80;
                    preamble |= (byte)(exponentLength - 1);

                    if (((ulong)longValue & 0x8000000000000000UL) != 0)
                    {
                        preamble |= 0x40; // Sign
                    }
                    output.WriteByte(preamble);

                    size++;
                    size += EncodeLong(output, exponent, exponentLength);                 // signed exponent
                    size += EncodeLong(output, mantissa, GetLongLength(mantissa, false)); // unsigned mantissa
                }
            }

            return(size);
        }
Ejemplo n.º 3
0
        public static int EncodeMultiByteInteger(IBerOutput output, uint value)
        {
            var size = 1;

            if ((value & 0xF0000000) != 0) // most significant 4 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 28) & 0x0F)));
                size++;
            }

            if ((value & 0xFFE00000) != 0) // most significant 11 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 21) & 0x7F)));
                size++;
            }

            if ((value & 0xFFFFC000) != 0) // most significant 18 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 14) & 0x7F)));
                size++;
            }

            if ((value & 0xFFFFFF80) != 0) // most significant 25 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 7) & 0x7F)));
                size++;
            }

            output.WriteByte((byte)(0x00 | ((value >> 0) & 0x7F)));

            return(size);
        }
Ejemplo n.º 4
0
        public EmberWriter(IBerOutput output)
        {
            if(output == null)
            throw new ArgumentNullException("output");

             _output = output;
        }
Ejemplo n.º 5
0
        internal override void EncodeOverride(IBerOutput output)
        {
            Debug.Assert(Value != null);

            base.EncodeOverride(output);

            BerEncoding.EncodeByteArray(output, Value);
        }
Ejemplo n.º 6
0
        internal override void EncodeOverride(IBerOutput output)
        {
            Debug.Assert(Value != null);

            base.EncodeOverride(output);

            BerEncoding.EncodeRelativeOid(output, Value);
        }
Ejemplo n.º 7
0
        public static int EncodeUtf8String(IBerOutput output, string value)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(value);

            output.WriteBytes(bytes);

            return(bytes.Length);
        }
Ejemplo n.º 8
0
        internal override void EncodeOverride(IBerOutput output)
        {
            Debug.Assert(Value != null);

            base.EncodeOverride(output);

            BerEncoding.EncodeObjectIdentifier(output, Value);
        }
Ejemplo n.º 9
0
        public static int EncodeAsciiString(IBerOutput output, string value)
        {
            byte[] bytes = AsciiEncoding.GetBytes(value);

            output.WriteBytes(bytes);

            return(bytes.Length);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Writes the binary BER representaion of this node to output.
        /// </summary>
        /// <param name="output">The output to write to.</param>
        internal override void EncodeOverride(IBerOutput output)
        {
            if (_encoded == null)
            {
                Update();
            }

            output.WriteBytes(_encoded);
        }
Ejemplo n.º 11
0
        public EmberWriter(IBerOutput output)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }

            _output = output;
        }
Ejemplo n.º 12
0
        public static int EncodeRelativeOid(IBerOutput output, int[] value)
        {
            var size = 0;

            foreach (var id in value)
            {
                size += EncodeMultiByteInteger(output, (uint)id);
            }

            return(size);
        }
Ejemplo n.º 13
0
        public static int EncodeInteger(IBerOutput output, int value, int length)
        {
            Debug.Assert(length > 0 && length <= 4);

            var dword = (uint)value;
            var bits  = length * 8;

            while (bits > 0)
            {
                bits -= 8;

                output.WriteByte((byte)((dword >> bits) & 0xFF));
            }

            return(length);
        }
Ejemplo n.º 14
0
        public static int EncodeLong(IBerOutput output, long value, int length)
        {
            Debug.Assert(length > 0 && length <= 8);

            ulong qword = (ulong)value;
            int   bits  = length * 8;

            while (bits > 0)
            {
                bits -= 8;

                output.WriteByte((byte)((qword >> bits) & 0xFF));
            }

            return(length);
        }
Ejemplo n.º 15
0
        public static int EncodeGeneralizedTime(IBerOutput output, DateTime value) // encoded as GeneralizedTime (ASCII)
        {
            var utcTime = value.ToUniversalTime();

            string sBuffer = String.Format(
                "{0:0000}{1:00}{2:00}{3:00}{4:00}{5:00}.{6}Z",
                utcTime.Year,
                utcTime.Month,
                utcTime.Day,
                utcTime.Hour,
                utcTime.Minute,
                utcTime.Second,
                utcTime.Millisecond);

            return(EncodeAsciiString(output, sBuffer));
        }
Ejemplo n.º 16
0
        public static int EncodeObjectIdentifier(IBerOutput output, int[] values)
        {
            var bytes = new List <byte>(4);
            var size  = 0;

            if (values.Length > 1)
            {
                size = EncodeObjectSubidentifier(output, values[0] * 40 + values[1], bytes);

                for (int index = 2; index < values.Length; index++)
                {
                    size += EncodeObjectSubidentifier(output, values[index], bytes);
                }
            }

            return(size);
        }
Ejemplo n.º 17
0
        internal override sealed void EncodeOverride(IBerOutput output)
        {
            if (IsDirty)
            {
                Update();
            }

            output.WriteBytes(_encodedFrameHeader);

            foreach (var node in this)
            {
                node.Encode(output);
            }

            output.WriteBytes(BerEncoding.IndefiniteLengthTerminator);
            output.Flush();
        }
Ejemplo n.º 18
0
        static int EncodeObjectSubidentifier(IBerOutput output, int value, List <byte> bytes)
        {
            var size = 1;

            bytes.Clear();
            bytes.Add((byte)(value & 0x7F));

            while ((value = value >> 7) > 0)
            {
                bytes.Add((byte)((value & 0x7F) | 0x80));
                size++;
            }

            bytes.Reverse();
            output.WriteBytes(bytes.ToArray());

            return(size);
        }
Ejemplo n.º 19
0
        // ====================================================================
        //
        // Encode functions
        // all return the number of bytes in the
        // encoded result.
        //
        // ====================================================================
        #region Encode Functions
        public static int EncodeTag(IBerOutput output, BerTag tag)
        {
            var number = tag.Number;
            var size   = 1;

            tag.Preamble &= 0xE0;

            if (number < 0x1F)
            {
                output.WriteByte((byte)(tag.Preamble | (number & 0x1F)));
            }
            else
            {
                output.WriteByte((byte)(tag.Preamble | 0x1F));

                size += EncodeMultiByteInteger(output, number);
            }

            return(size);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Writes the binary BER representaion of this node and its children to output.
        /// </summary>
        /// <param name="output">The output to write to.</param>
        internal override void EncodeOverride(IBerOutput output)
        {
            if (IsDirty)
            {
                Update();
            }

            output.WriteBytes(_encodedHeader);

            var orderedNodes = OrderChildren(_nodes);

            foreach (var node in orderedNodes)
            {
                node.Encode(output);
            }

            if (Parent == null) // root node
            {
                output.Flush();
            }
        }
Ejemplo n.º 21
0
        // encoded as GeneralizedTime (ASCII)
        public static int EncodeGeneralizedTime(IBerOutput output, DateTime value)
        {
            var utcTime = value.ToUniversalTime();

             string sBuffer = String.Format(
            "{0:0000}{1:00}{2:00}{3:00}{4:00}{5:00}.{6}Z",
            utcTime.Year,
            utcTime.Month,
            utcTime.Day,
            utcTime.Hour,
            utcTime.Minute,
            utcTime.Second,
            utcTime.Millisecond);

             return EncodeAsciiString(output, sBuffer);
        }
Ejemplo n.º 22
0
        public static int EncodeByteArray(IBerOutput output, byte[] value)
        {
            output.WriteBytes(value);

            return(value.Length);
        }
Ejemplo n.º 23
0
        public static int EncodeInteger(IBerOutput output, int value, int length)
        {
            Debug.Assert(length > 0 && length <= 4);

             var dword = (uint)value;
             var bits = length * 8;

             while(bits > 0)
             {
            bits -= 8;

            output.WriteByte((byte)((dword >> bits) & 0xFF));
             }

             return length;
        }
Ejemplo n.º 24
0
        public static int EncodeLength(IBerOutput output, int value)
        {
            int size = 1;

             if(value == BerDefinitions.IndefiniteLength)
             {
            output.WriteByte(0x80);
             }
             else
             {
            if(value <= 0x7F)
            {
               output.WriteByte((byte)(value & 0x7F));
            }
            else
            {
               int integerLength = GetIntegerLength((int)value);
               output.WriteByte((byte)(0x80 | integerLength));

               size += EncodeInteger(output, (int)value, integerLength);
            }
             }

             return size;
        }
Ejemplo n.º 25
0
 static void EncodeHeader(IBerOutput output, BerTag tag, int length)
 {
     BerEncoding.EncodeTag(output, tag);
      BerEncoding.EncodeLength(output, length);
 }
Ejemplo n.º 26
0
        public static int EncodeReal(IBerOutput output, double value)
        {
            var size = 0;

             if(double.IsPositiveInfinity(value))
             {
            output.WriteByte(0x40); // 01000000 Value is PLUS-INFINITY
            size = 1;
             }
             else if(double.IsNegativeInfinity(value)) // negative infinity
             {
            output.WriteByte(0x41); // 01000001 Value is MINUS-INFINITY
            size = 1;
             }
             else
             {
            long longValue = DoubleToInt64Bits(value);

            if(longValue != 0)
            {
               long exponent = ((0x7FF0000000000000L & longValue) >> 52) - 1023;
               long mantissa =   0x000FFFFFFFFFFFFFL & longValue;
               mantissa |=       0x0010000000000000L; // set virtual delimeter

               // normalize mantissa (required by CER and DER)
               while((mantissa & 0xFF) == 0)
                  mantissa >>= 8;

               while((mantissa & 0x01) == 0)
                  mantissa >>= 1;

               int exponentLength = GetLongLength(exponent);
               Debug.Assert(exponentLength <= 3);

               byte preamble = 0x80;
               preamble |= (byte)(exponentLength - 1);

               if(((ulong)longValue & 0x8000000000000000UL) != 0)
                  preamble |= 0x40; // Sign

               output.WriteByte(preamble);

               size++;
               size += EncodeLong(output, exponent, exponentLength); // signed exponent
               size += EncodeLong(output, mantissa, GetLongLength(mantissa, false)); // unsigned mantissa
            }
             }

             return size;
        }
Ejemplo n.º 27
0
        public static int EncodeAsciiString(IBerOutput output, string value)
        {
            byte[] bytes = AsciiEncoding.GetBytes(value);

             output.WriteBytes(bytes);

             return bytes.Length;
        }
Ejemplo n.º 28
0
 public static int EncodeBoolean(IBerOutput output, bool value)
 {
     output.WriteByte((byte)(value ? 0xFF : 0));
     return(1);
 }
Ejemplo n.º 29
0
        public static int EncodeMultiByteLong(IBerOutput output, ulong value)
        {
            var size = 1;

             if((value & 0x8000000000000000UL) != 0) // most significant 1 bits
             {
            output.WriteByte((byte)(0x80 | 1));
            size++;
             }

             if((value & 0xFF00000000000000UL) != 0) // most significant 8 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 56) & 0x7F)));
            size++;
             }

             if((value & 0xFFFE000000000000UL) != 0) // most significant 15 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 49) & 0x7F)));
            size++;
             }

             if((value & 0xFFFFFC0000000000UL) != 0) // most significant 22 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 42) & 0x7F)));
            size++;
             }

             if((value & 0xFFFFFFF800000000UL) != 0) // most significant 29 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 35) & 0x7F)));
            size++;
             }

             if((value & 0xFFFFFFFFF0000000UL) != 0) // most significant 36 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 28) & 0x7F)));
            size++;
             }

             if((value & 0xFFFFFFFFFFE00000UL) != 0) // most significant 43 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 21) & 0x7F)));
            size++;
             }

             if((value & 0xFFFFFFFFFFFFC000UL) != 0) // most significant 50 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 14) & 0x7F)));
            size++;
             }

             if((value & 0xFFFFFFFFFFFFFF80UL) != 0) // most significant 57 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 7) & 0x7F)));
            size++;
             }

             output.WriteByte((byte)(0x00 | ((value >> 0) & 0x7F)));

             return size;
        }
Ejemplo n.º 30
0
        public static int EncodeMultiByteLong(IBerOutput output, ulong value)
        {
            var size = 1;

            if ((value & 0x8000000000000000UL) != 0) // most significant 1 bits
            {
                output.WriteByte((byte)(0x80 | 1));
                size++;
            }

            if ((value & 0xFF00000000000000UL) != 0) // most significant 8 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 56) & 0x7F)));
                size++;
            }

            if ((value & 0xFFFE000000000000UL) != 0) // most significant 15 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 49) & 0x7F)));
                size++;
            }

            if ((value & 0xFFFFFC0000000000UL) != 0) // most significant 22 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 42) & 0x7F)));
                size++;
            }

            if ((value & 0xFFFFFFF800000000UL) != 0) // most significant 29 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 35) & 0x7F)));
                size++;
            }

            if ((value & 0xFFFFFFFFF0000000UL) != 0) // most significant 36 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 28) & 0x7F)));
                size++;
            }

            if ((value & 0xFFFFFFFFFFE00000UL) != 0) // most significant 43 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 21) & 0x7F)));
                size++;
            }

            if ((value & 0xFFFFFFFFFFFFC000UL) != 0) // most significant 50 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 14) & 0x7F)));
                size++;
            }

            if ((value & 0xFFFFFFFFFFFFFF80UL) != 0) // most significant 57 bits
            {
                output.WriteByte((byte)(0x80 | ((value >> 7) & 0x7F)));
                size++;
            }

            output.WriteByte((byte)(0x00 | ((value >> 0) & 0x7F)));

            return(size);
        }
Ejemplo n.º 31
0
 /// <summary>
 /// Writes the binary BER representaion of this node and its children to <paramref name="output"/>.
 /// </summary>
 /// <param name="output">The output to write to.</param>
 public void Encode(IBerOutput output)
 {
     EncodeOverride(output);
 }
Ejemplo n.º 32
0
        public static int EncodeObjectIdentifier(IBerOutput output, int[] values)
        {
            var bytes = new List<byte>(4);
             var size = 0;

             if(values.Length > 1)
             {
            size = EncodeObjectSubidentifier(output, values[0] * 40 + values[1], bytes);

            for(int index = 2; index < values.Length; index++)
               size += EncodeObjectSubidentifier(output, values[index], bytes);
             }

             return size;
        }
Ejemplo n.º 33
0
 static void EncodeHeader(IBerOutput output, BerTag tag, int length)
 {
     BerEncoding.EncodeTag(output, tag);
     BerEncoding.EncodeLength(output, length);
 }
Ejemplo n.º 34
0
        public static int EncodeByteArray(IBerOutput output, byte[] value)
        {
            output.WriteBytes(value);

             return value.Length;
        }
Ejemplo n.º 35
0
        public static int EncodeMultiByteInteger(IBerOutput output, uint value)
        {
            var size = 1;

             if((value & 0xF0000000) != 0) // most significant 4 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 28) & 0x0F)));
            size++;
             }

             if((value & 0xFFE00000) != 0) // most significant 11 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 21) & 0x7F)));
            size++;
             }

             if((value & 0xFFFFC000) != 0) // most significant 18 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 14) & 0x7F)));
            size++;
             }

             if((value & 0xFFFFFF80) != 0) // most significant 25 bits
             {
            output.WriteByte((byte)(0x80 | ((value >> 7) & 0x7F)));
            size++;
             }

             output.WriteByte((byte)(0x00 | ((value >> 0) & 0x7F)));

             return size;
        }
Ejemplo n.º 36
0
 // value encodings
 public static int EncodeBoolean(IBerOutput output, bool value)
 {
     output.WriteByte((byte)(value ? 0xFF : 0));
      return 1;
 }
Ejemplo n.º 37
0
        public static int EncodeLong(IBerOutput output, long value, int length)
        {
            Debug.Assert(length > 0 && length <= 8);

             ulong qword = (ulong)value;
             int bits = length * 8;

             while(bits > 0)
             {
            bits -= 8;

            output.WriteByte((byte)((qword >> bits) & 0xFF));
             }

             return length;
        }
Ejemplo n.º 38
0
        static int EncodeObjectSubidentifier(IBerOutput output, int value, List<byte> bytes)
        {
            var size = 1;

             bytes.Clear();
             bytes.Add((byte)(value & 0x7F));

             while((value = value >> 7) > 0)
             {
            bytes.Add((byte)((value & 0x7F) | 0x80));
            size++;
             }

             bytes.Reverse();
             output.WriteBytes(bytes.ToArray());

             return size;
        }
Ejemplo n.º 39
0
        public static int EncodeRelativeOid(IBerOutput output, int[] value)
        {
            var size = 0;

             foreach(var id in value)
            size += EncodeMultiByteInteger(output, (uint)id);

             return size;
        }
Ejemplo n.º 40
0
        // ====================================================================
        //
        // Encode functions
        // all return the number of bytes in the
        // encoded result.
        //
        // ====================================================================
        public static int EncodeTag(IBerOutput output, BerTag tag)
        {
            var number = tag.Number;
             var size = 1;
             tag.Preamble &= 0xE0;

             if(number < 0x1F)
             {
            output.WriteByte((byte)(tag.Preamble | (number & 0x1F)));
             }
             else
             {
            output.WriteByte((byte)(tag.Preamble | 0x1F));

            size += EncodeMultiByteInteger(output, number);
             }

             return size;
        }
Ejemplo n.º 41
0
 /// <summary>
 /// Writes the binary BER representaion of this node and its children to <paramref name="output"/>.
 /// </summary>
 /// <param name="output">The output to write to.</param>
 internal abstract void EncodeOverride(IBerOutput output);
Ejemplo n.º 42
0
        public static int EncodeUtf8String(IBerOutput output, string value)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(value);

             output.WriteBytes(bytes);

             return bytes.Length;
        }
Ejemplo n.º 43
0
        internal override sealed void EncodeOverride(IBerOutput output)
        {
            if(IsDirty)
            Update();

             output.WriteBytes(_encodedFrameHeader);

             foreach(var node in this)
            node.Encode(output);

             output.WriteBytes(BerEncoding.IndefiniteLengthTerminator);
             output.Flush();
        }