/// <summary>
 /// Decodes the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 /// <remarks>Length is included.</remarks>
 protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     if (Constraint != null && Constraint.HasMinSize && Constraint.HasMinSize &&
         Constraint.MinSize == Constraint.MaxSize && Constraint.MinSize <= 2)    //Ref. X.691:16.6
     {
         long   minSize    = Constraint.MinSize;
         bool[] bitResult  = buffer.ReadBits((int)(8 * minSize));
         byte[] byteResult = new byte[(int)minSize];
         //Convert bool array to byte array
         int byteIndex = 0, bitIndex = 0;
         foreach (bool b in bitResult)
         {
             if (b)
             {
                 byteResult[byteIndex] |= (byte)(1 << (7 - bitIndex));
             }
             bitIndex++;
             if (bitIndex == 8)
             {
                 bitIndex = 0;
                 byteIndex++;
             }
         }
         ByteArrayValue = byteResult;
     }
     else
     {
         ByteArrayValue = Asn1StandardProcedure.PerDecodeArray(buffer,
                                                               decodingBuffer => decodingBuffer.ReadByte(),
                                                               Constraint != null && Constraint.HasMinSize ? Constraint.MinSize : (long?)null, Constraint != null && Constraint.HasMaxSize ? Constraint.MaxSize : (long?)null);
     }
 }
        /// <summary>
        /// Decodes the content of the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            int optionalFieldsCount = 0;

            foreach (var v in fieldsMetaData)
            {
                if (v.Optional)
                {
                    optionalFieldsCount++;
                }
            }
            if (optionalFieldsCount >= 64 * 1024) //64k
            {
                throw new NotImplementedException("More than 64K optional fields are not supported yet.");
                //Ref. X.691: 18.3
            }
            bool[]       bitMap               = buffer.ReadBits(optionalFieldsCount);
            Asn1Object[] decodingResult       = FieldsTypeInstances;
            int          curOptionalFlagIndex = 0; //index in bitMap

            for (int i = 0; i < decodingResult.Length; i++)
            {
                if (fieldsMetaData[i].Optional == false ||
                    fieldsMetaData[i].Optional && bitMap[curOptionalFlagIndex++])
                {
                    decodingResult[i].PerDecode(buffer, aligned);
                }
                else
                {
                    //fieldOptionalFlags[i] equals to true and bitMap[curOptionalFlagIndex] equals to false.
                    decodingResult[i] = null;
                }
            }

            for (int i = 0; i < decodingResult.Length; i++)
            {
                fieldsMetaData[i].ValueInOutObject = decodingResult[i];
            }
        }
Ejemplo n.º 3
0
 public override void PerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     Value = buffer.ReadBits(2);
 }
        /// <summary>
        /// Decodes the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        public override void PerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            if (aligned == false)
            {
                throw new NotImplementedException(ExceptionMessages.UnalignedNotImplemented);
            }
            long baseNum = Min ?? 0;
            //Ref. X.691: 10.5.2
            if (Min == null || Max == null) //range is equal to null
            {
                byte length = buffer.ReadByte();
                if (length == 0)
                {
                    Value = baseNum;
                    return;
                }

                byte[] result = buffer.ReadBytes(length);
                if (Min == null)
                {
                    //No base
                    Value = IntegerDecoding(result);
                }
                else
                {
                    Value = baseNum + NonNegativeBinaryIntegerPerDeocde(result);
                }
            }
            else
            {
                if (Range == 1)
                {
                    this.Value = Min;
                }
                else if (Range <= 255) //10.5.7: a)
                {
                    int bitFieldSize = 1;
                    while (Range > (1 << (bitFieldSize)))
                    {
                        bitFieldSize++;
                    }
                    bool[] result = buffer.ReadBits(bitFieldSize);
                    byte offset = 0;
                    foreach (var b in result)
                    {
                        offset <<= 1;
                        offset += (byte)(b ? 1 : 0);
                    }
                    Value = baseNum + offset;
                }
                else if (Range == 256) //10.5.7: b)
                {
                    Value = baseNum + buffer.ReadByte();
                }
                else if (Range <= 256 * 256) //10.5.7: c)
                {
                    byte[] bytes = buffer.ReadBytes(2);
                    Value = baseNum + bytes[0] * 256 + bytes[1];
                }
                else //10.5.7: d)
                {
                    Asn1Integer len = new Asn1Integer();
                    len.Min = 1;
                    len.Max = 4;
                    len.PerDecode(buffer);
                    Debug.Assert(len.Value != null, "len.Value != null");
                    byte[] bytes = buffer.ReadBytes((int)len.Value);
                    Value = baseNum + NonNegativeBinaryIntegerPerDeocde(bytes);
                }
            }

            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Decodes the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        public override void PerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            if (aligned == false)
            {
                throw new NotImplementedException(ExceptionMessages.UnalignedNotImplemented);
            }
            long baseNum = Min ?? 0;

            //Ref. X.691: 10.5.2
            if (Min == null || Max == null) //range is equal to null
            {
                byte length = buffer.ReadByte();
                if (length == 0)
                {
                    Value = baseNum;
                    return;
                }

                byte[] result = buffer.ReadBytes(length);
                if (Min == null)
                {
                    //No base
                    Value = IntegerDecoding(result);
                }
                else
                {
                    Value = baseNum + NonNegativeBinaryIntegerPerDeocde(result);
                }
            }
            else
            {
                if (Range == 1)
                {
                    this.Value = Min;
                }
                else if (Range <= 255) //10.5.7: a)
                {
                    int bitFieldSize = 1;
                    while (Range > (1 << (bitFieldSize)))
                    {
                        bitFieldSize++;
                    }
                    bool[] result = buffer.ReadBits(bitFieldSize);
                    byte   offset = 0;
                    foreach (var b in result)
                    {
                        offset <<= 1;
                        offset  += (byte)(b ? 1 : 0);
                    }
                    Value = baseNum + offset;
                }
                else if (Range == 256) //10.5.7: b)
                {
                    Value = baseNum + buffer.ReadByte();
                }
                else if (Range <= 256 * 256) //10.5.7: c)
                {
                    byte[] bytes = buffer.ReadBytes(2);
                    Value = baseNum + bytes[0] * 256 + bytes[1];
                }
                else //10.5.7: d)
                {
                    Asn1Integer len = new Asn1Integer();
                    len.Min = 1;
                    len.Max = 4;
                    len.PerDecode(buffer);
                    Debug.Assert(len.Value != null, "len.Value != null");
                    byte[] bytes = buffer.ReadBytes((int)len.Value);
                    Value = baseNum + NonNegativeBinaryIntegerPerDeocde(bytes);
                }
            }

            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied);
            }
        }
 public override void PerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     Value = buffer.ReadBits(2);
 }
 /// <summary>
 /// Decodes the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 /// <remarks>Length is included.</remarks>
 protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     if (Constraint != null && Constraint.HasMinSize && Constraint.HasMinSize
         && Constraint.MinSize == Constraint.MaxSize && Constraint.MinSize <= 2) //Ref. X.691:16.6
     {
         long minSize = Constraint.MinSize;
         bool[] bitResult = buffer.ReadBits((int)(8 * minSize));
         byte[] byteResult = new byte[(int)minSize];
         //Convert bool array to byte array
         int byteIndex = 0, bitIndex = 0;
         foreach (bool b in bitResult)
         {
             if (b)
             {
                 byteResult[byteIndex] |= (byte)(1 << (7 - bitIndex));
             }
             bitIndex++;
             if (bitIndex == 8)
             {
                 bitIndex = 0;
                 byteIndex++;
             }
         }
         ByteArrayValue = byteResult;
     }
     else
     {
         ByteArrayValue = Asn1StandardProcedure.PerDecodeArray(buffer,
         decodingBuffer => decodingBuffer.ReadByte(),
         Constraint != null && Constraint.HasMinSize ? Constraint.MinSize : (long?)null, Constraint != null && Constraint.HasMaxSize ? Constraint.MaxSize : (long?)null);
     }
 }
        /// <summary>
        /// Decodes the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            long baseNum = (Constraints != null && Constraints.HasMin ? Constraints.Min : 0);
            //Ref. X.691: 10.5.2
            if (Range == null) //range is equal to null
            {
                byte length = buffer.ReadByte();
                if (length == 0)
                {
                    Value = baseNum;
                    return;
                }

                byte[] result = buffer.ReadBytes(length);
                if (Constraints == null || !Constraints.HasMin)
                {
                    //No base
                    Value = IntegerDecoding(result);
                }
                else
                {
                    Value = baseNum + NonNegativeBinaryIntegerPerDeocde(result);
                }
            }
            else
            {
                if (Range == 1)
                {
                    Value = Constraints.Min;
                }
                else if (Range <= 255) //10.5.7: a)
                {
                    int bitFieldSize = 1;
                    while (Range > (1 << (bitFieldSize)))
                    {
                        bitFieldSize++;
                    }
                    bool[] result = buffer.ReadBits(bitFieldSize);
                    byte offset = 0;
                    foreach (var b in result)
                    {
                        offset <<= 1;
                        offset += (byte)(b ? 1 : 0);
                    }
                    Value = baseNum + offset;
                }
                else if (Range == 256) //10.5.7: b)
                {
                    Value = baseNum + buffer.ReadByte();
                }
                else if (Range <= 256 * 256) //10.5.7: c)
                {
                    byte[] bytes = buffer.ReadBytes(2);
                    Value = baseNum + bytes[0] * 256 + bytes[1];
                }
                else //10.5.7: d)
                {
                    Asn1Integer len = new Asn1Integer { Constraints = new Asn1IntegerBound { Min = 1, Max = 4 } };
                    len.PerDecode(buffer);
                    byte[] bytes = buffer.ReadBytes((int)len.Value);
                    Value = baseNum + NonNegativeBinaryIntegerPerDeocde(bytes);
                }
            }
        }
        /// <summary>
        /// Decodes the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            long baseNum = (Constraints != null && Constraints.HasMin ? Constraints.Min : 0);

            //Ref. X.691: 10.5.2
            if (Range == null) //range is equal to null
            {
                byte length = buffer.ReadByte();
                if (length == 0)
                {
                    Value = baseNum;
                    return;
                }

                byte[] result = buffer.ReadBytes(length);
                if (Constraints == null || !Constraints.HasMin)
                {
                    //No base
                    Value = IntegerDecoding(result);
                }
                else
                {
                    Value = baseNum + NonNegativeBinaryIntegerPerDeocde(result);
                }
            }
            else
            {
                if (Range == 1)
                {
                    Value = Constraints.Min;
                }
                else if (Range <= 255) //10.5.7: a)
                {
                    int bitFieldSize = 1;
                    while (Range > (1 << (bitFieldSize)))
                    {
                        bitFieldSize++;
                    }
                    bool[] result = buffer.ReadBits(bitFieldSize);
                    byte   offset = 0;
                    foreach (var b in result)
                    {
                        offset <<= 1;
                        offset  += (byte)(b ? 1 : 0);
                    }
                    Value = baseNum + offset;
                }
                else if (Range == 256) //10.5.7: b)
                {
                    Value = baseNum + buffer.ReadByte();
                }
                else if (Range <= 256 * 256) //10.5.7: c)
                {
                    byte[] bytes = buffer.ReadBytes(2);
                    Value = baseNum + bytes[0] * 256 + bytes[1];
                }
                else //10.5.7: d)
                {
                    Asn1Integer len = new Asn1Integer {
                        Constraints = new Asn1IntegerBound {
                            Min = 1, Max = 4
                        }
                    };
                    len.PerDecode(buffer);
                    byte[] bytes = buffer.ReadBytes((int)len.Value);
                    Value = baseNum + NonNegativeBinaryIntegerPerDeocde(bytes);
                }
            }
        }
        /// <summary>
        /// Decodes the content of the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            int optionalFieldsCount = 0;
            foreach (var v in fieldsMetaData)
            {
                if (v.Optional)
                {
                    optionalFieldsCount++;
                }
            }
            if (optionalFieldsCount >= 64 * 1024) //64k
            {
                throw new NotImplementedException("More than 64K optional fields are not supported yet.");
                //Ref. X.691: 18.3
            }
            bool[] bitMap = buffer.ReadBits(optionalFieldsCount);
            Asn1Object[] decodingResult = FieldsTypeInstances;
            int curOptionalFlagIndex = 0; //index in bitMap
            for (int i = 0; i < decodingResult.Length; i++)
            {
                if (fieldsMetaData[i].Optional == false ||
                    fieldsMetaData[i].Optional && bitMap[curOptionalFlagIndex++])
                {
                    decodingResult[i].PerDecode(buffer, aligned);
                }
                else
                {
                    //fieldOptionalFlags[i] equals to true and bitMap[curOptionalFlagIndex] equals to false.
                    decodingResult[i] = null;
                }
            }

            for (int i = 0; i < decodingResult.Length; i++)
            {
                fieldsMetaData[i].ValueInOutObject = decodingResult[i];
            }
        }