Exemplo n.º 1
0
        public override int encodeEnumItem(object enumConstant, Type enumClass, Stream stream, ElementInfo elementInfo)
        {
            ASN1EnumItem enumObj = elementInfo.getAttribute <ASN1EnumItem>();
            //int min = 0, max = enumClass.GetFields().Length, val = 0;
            int min = 0, max = 0, val = 0;

            foreach (FieldInfo enumItem in enumClass.GetFields())
            {
                if (CoderUtils.isAttributePresent <ASN1EnumItem>(enumItem))
                {
                    ASN1EnumItem enumItemObj = CoderUtils.getAttribute <ASN1EnumItem>(enumItem);
                    if (enumItemObj.Tag == enumObj.Tag)
                    {
                        val = max;
                    }
                    max++; //val++;
                }
            }
            if (max > 0)
            {
                return(encodeConstraintNumber(val, min, max, (BitArrayOutputStream)stream));
            }
            else
            {
                throw new Exception("Unable to present any enum item!");
            }
        }
Exemplo n.º 2
0
 public override void setParentAnnotated(ICustomAttributeProvider parent)
 {
     if (parent != null)
     {
         if (CoderUtils.isAttributePresent <ASN1String>(parent))
         {
             ASN1String value = CoderUtils.getAttribute <ASN1String>(parent);
             stringType = value.StringType;
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        ///     Encoding of the choice structure
        ///     ITU-T X.691. 22.
        ///     NOTE – (Tutorial) A choice type is encoded by encoding an index specifying
        ///     the chosen alternative. This is encoded as for a constrained integer
        ///     (unless the extension marker is present in the choice type,
        ///     in which case it is a normally small non-negative whole number)
        ///     and would therefore typically occupy a fixed length bit-field of the
        ///     minimum number of bits needed to encode the index. (Although it could
        ///     in principle be arbitrarily large.) This is followed by the encoding
        ///     of the chosen alternative, with alternatives that are extension
        ///     additions encoded as if they were the value of an open type field.
        ///     Where the choice has only one alternative, there is no encoding
        ///     for the index.
        /// </summary>
        public override int encodeChoice(object obj, Stream stream, ElementInfo elementInfo)
        {
            int resultSize = 0;

            doAlign(stream);
            ElementInfo info = null;

            int elementIndex = 0;
            int fieldIdx     = 0;

            foreach (PropertyInfo field in elementInfo.getProperties(obj.GetType()))
            {
                info = new ElementInfo();
                info.AnnotatedClass = field;

                elementIndex++;
                if (elementInfo.hasPreparedInfo())
                {
                    info.PreparedInfo = elementInfo.PreparedInfo.getPropertyMetadata(fieldIdx);
                }
                else
                {
                    info.ASN1ElementInfo = CoderUtils.getAttribute <ASN1Element>(field);
                }

                if (invokeSelectedMethodForField(field, obj, info))
                {
                    break;
                }
                else
                {
                    info = null;
                }
                fieldIdx++;
            }
            if (info == null)
            {
                throw new ArgumentException("The choice '" + obj + "' does not have a selected item!");
            }
            object invokeObjResult = invokeGetterMethodForField((PropertyInfo)info.AnnotatedClass, obj, info);

            resultSize += encodeChoicePreamble(obj, stream, elementIndex, elementInfo);
            resultSize += encodeClassType(invokeObjResult, stream, info);
            return(resultSize);
        }
Exemplo n.º 4
0
        public override DecodedObject <object> decodeChoice(DecodedObject <object> decodedTag, Type objectClass, ElementInfo elementInfo, Stream stream)
        {
            object choice = createInstanceForElement(objectClass, elementInfo);

            skipAlignedBits(stream);
            PropertyInfo[]         fields       = elementInfo.getProperties(objectClass);
            int                    elementIndex = (int)decodeConstraintNumber(1, fields.Length, (BitArrayInputStream)stream);
            DecodedObject <object> val          = null;

            for (int i = 0; i < elementIndex && i < fields.Length; i++)
            {
                if (i + 1 == elementIndex)
                {
                    PropertyInfo field = fields[i];
                    ElementInfo  info  = new ElementInfo();
                    info.AnnotatedClass = field;
                    if (elementInfo.hasPreparedInfo())
                    {
                        info.PreparedInfo = elementInfo.PreparedInfo.getPropertyMetadata(i);
                    }
                    else
                    {
                        info.ASN1ElementInfo = CoderUtils.getAttribute <ASN1Element>(field);
                    }
                    val = decodeClassType(decodedTag, field.PropertyType, info, stream);
                    if (val != null)
                    {
                        invokeSelectMethodForField(field, choice, val.Value, info);
                    }
                    break;
                }
                ;
            }
            if (val == null && !CoderUtils.isOptional(elementInfo))
            {
                throw new ArgumentException("The choice '" + objectClass + "' does not have a selected item!");
            }
            else
            {
                return(new DecodedObject <object>(choice));
            }
        }
Exemplo n.º 5
0
        public override DecodedObject <object> decodeEnumItem(DecodedObject <object> decodedTag, Type objectClass, Type enumClass, ElementInfo elementInfo, Stream stream)
        {
            //int min = 0, max = enumClass.GetFields().Length;
            int min = 0, max = 0;

            foreach (FieldInfo enumItem in enumClass.GetFields())
            {
                if (CoderUtils.isAttributePresent <ASN1EnumItem>(enumItem))
                {
                    max++;
                }
            }

            if (max <= 0)
            {
                throw new Exception("Unable to present any enum item!");
            }

            int enumItemIdx = (int)decodeConstraintNumber(min, max - 1, (BitArrayInputStream)stream);
            DecodedObject <object> result = new DecodedObject <object>();
            int idx = 0;

            foreach (FieldInfo enumItem in enumClass.GetFields())
            {
                if (CoderUtils.isAttributePresent <ASN1EnumItem>(enumItem))
                {
                    if (idx++ == enumItemIdx)
                    {
                        ASN1EnumItem enumItemObj = CoderUtils.getAttribute <ASN1EnumItem>(enumItem);
                        result.Value = enumItemObj.Tag;
                        break;
                    }
                }
            }
            return(result);
        }
Exemplo n.º 6
0
        protected virtual DecodedObject <object> decodeSet(DecodedObject <object> decodedTag, System.Type objectClass, ElementInfo elementInfo, int len, System.IO.Stream stream)
        {
            object sequence = createInstanceForElement(objectClass, elementInfo);

            initDefaultValues(sequence);
            DecodedObject <object> fieldTag = null;
            int sizeOfSequence = 0;
            int maxSeqLen      = elementInfo.MaxAvailableLen;

            if (maxSeqLen == -1 || maxSeqLen > 0)
            {
                fieldTag = decodeTag(stream);
                if (fieldTag != null)
                {
                    sizeOfSequence += fieldTag.Size;
                }
            }
            PropertyInfo[] fields =
                elementInfo.getProperties(objectClass);

            bool fieldEncoded = false;

            do
            {
                for (int i = 0; i < fields.Length; i++)
                {
                    PropertyInfo           field = fields[i];
                    DecodedObject <object> obj   = decodeSequenceField(
                        fieldTag, sequence, i, field, stream, elementInfo, false
                        );
                    if (obj != null)
                    {
                        fieldEncoded    = true;
                        sizeOfSequence += obj.Size;

                        bool isAny = false;
                        if (i + 1 == fields.Length - 1)
                        {
                            ElementInfo info = new ElementInfo();
                            info.AnnotatedClass  = (fields[i + 1]);
                            info.MaxAvailableLen = (elementInfo.MaxAvailableLen);
                            if (elementInfo.hasPreparedInfo())
                            {
                                info.PreparedInfo = (elementInfo.PreparedInfo.getPropertyMetadata(i + 1));
                            }
                            else
                            {
                                info.ASN1ElementInfo = CoderUtils.getAttribute <ASN1Element>(fields[i + 1]);
                            }
                            isAny = CoderUtils.isAnyField(fields[i + 1], info);
                        }

                        if (maxSeqLen != -1)
                        {
                            elementInfo.MaxAvailableLen = (maxSeqLen - sizeOfSequence);
                        }

                        if (!isAny)
                        {
                            if (i < fields.Length - 1)
                            {
                                if (maxSeqLen == -1 || elementInfo.MaxAvailableLen > 0)
                                {
                                    fieldTag = decodeTag(stream);
                                    if (fieldTag != null)
                                    {
                                        sizeOfSequence += fieldTag.Size;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                else
                                {
                                    fieldTag = null;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    ;
                }
            }while (sizeOfSequence < len && fieldEncoded);
            return(new DecodedObject <object>(sequence, sizeOfSequence));
        }