コード例 #1
0
        public virtual int encodeBoxedType(object obj, System.IO.Stream stream, ElementInfo elementInfo)
        {
            PropertyInfo field = obj.GetType().GetProperty("Value");

            elementInfo.AnnotatedClass = field;

            if (elementInfo.ASN1ElementInfo == null)
            {
                elementInfo.ASN1ElementInfo = CoderUtils.getAttribute <ASN1Element>(field);
            }
            else
            {
                if (!elementInfo.ASN1ElementInfo.HasTag)
                {
                    ASN1Element fieldInfo = CoderUtils.getAttribute <ASN1Element>(field);
                    if (fieldInfo != null && fieldInfo.HasTag)
                    {
                        elementInfo.ASN1ElementInfo.HasTag        = true;
                        elementInfo.ASN1ElementInfo.TagClass      = fieldInfo.TagClass;
                        elementInfo.ASN1ElementInfo.IsImplicitTag = fieldInfo.IsImplicitTag;
                        elementInfo.ASN1ElementInfo.Tag           = fieldInfo.Tag;
                    }
                }
            }
            if (CoderUtils.isAttributePresent <ASN1Null>(field))
            {
                return(encodeNull(obj, stream, elementInfo));
            }
            else
            {
                return(encodeClassType(invokeGetterMethodForField(field, obj, elementInfo), stream, elementInfo));
            }
        }
コード例 #2
0
        public virtual int encodeSequenceField(object obj, int fieldIdx, PropertyInfo field, System.IO.Stream stream, ElementInfo elementInfo)
        {
            ElementInfo info = new ElementInfo();

            info.AnnotatedClass = field;

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

            int resultSize = 0;

            if (CoderUtils.isNullField(field, info))
            {
                return(encodeNull(obj, stream, elementInfo));
            }
            else
            {
                object invokeObjResult = invokeGetterMethodForField(field, obj, info);
                if (invokeObjResult != null)
                {
                    resultSize += encodeClassType(invokeObjResult, stream, info);
                }
                else
                {
                    CoderUtils.checkForOptionalField(field, info);
                }
            }
            return(resultSize);
        }
コード例 #3
0
        public virtual T decode <T>(Stream stream)
        {
            Type objectClass = typeof(T);

            ElementInfo elemInfo = new ElementInfo();

            elemInfo.AnnotatedClass = objectClass;
            object objectInstance = null;

            if (CoderUtils.isImplements(objectClass, (typeof(IASN1PreparedElement))))
            {
                objectInstance = createInstanceForElement(objectClass, elemInfo);
            }

            if (objectInstance != null && objectInstance is IASN1PreparedElement)
            {
                elemInfo.PreparedInstance = objectInstance;
                return((T)decodePreparedElement(decodeTag(stream), objectClass, elemInfo, stream).Value);
            }
            else
            {
                elemInfo.ASN1ElementInfo = CoderUtils.getAttribute <ASN1Element>(objectClass);
                return((T)decodeClassType(decodeTag(stream), objectClass, elemInfo, stream).Value);
            }
        }
コード例 #4
0
        public virtual void encode <T>(T obj, System.IO.Stream stream)
        {
            object      ob       = (object)obj;
            ElementInfo elemInfo = new ElementInfo();

            elemInfo.AnnotatedClass = obj.GetType();
            int sizeOfEncodedBytes = 0;

            if (obj is IASN1PreparedElement)
            {
                sizeOfEncodedBytes = encodePreparedElement(obj, stream, elemInfo);
            }
            else
            {
                elemInfo.ASN1ElementInfo = CoderUtils.getAttribute <ASN1Element>(obj.GetType());
                sizeOfEncodedBytes       = encodeClassType(obj, stream, elemInfo);
            }


            if (sizeOfEncodedBytes == 0)
            {
                throw new System.ArgumentException("Unable to find any supported annotation for class type: " + obj.GetType().ToString());
            }
            ;
        }
コード例 #5
0
 private void setupConstraint(ICustomAttributeProvider annotated)
 {
     if (CoderUtils.isAttributePresent <ASN1SizeConstraint>(annotated))
     {
         constraint = new ASN1SizeConstraintMetadata(CoderUtils.getAttribute <ASN1SizeConstraint> (annotated));
     }
     else
     if (CoderUtils.isAttributePresent <ASN1ValueRangeConstraint>(annotated))
     {
         constraint = new ASN1ValueRangeConstraintMetadata(CoderUtils.getAttribute <ASN1ValueRangeConstraint>(annotated));
     }
 }
コード例 #6
0
        public virtual DecodedObject <object> decodeBoxedType(DecodedObject <object> decodedTag, System.Type objectClass, ElementInfo elementInfo, System.IO.Stream stream)
        {
            object resultObj = createInstanceForElement(objectClass, elementInfo);

            DecodedObject <object> result = new DecodedObject <object>(resultObj);

            PropertyInfo field = objectClass.GetProperty("Value");

            if (elementInfo.ASN1ElementInfo == null)
            {
                elementInfo.ASN1ElementInfo = CoderUtils.getAttribute <ASN1Element>(field);
            }
            else
            {
                if (!elementInfo.ASN1ElementInfo.HasTag)
                {
                    ASN1Element fieldInfo = CoderUtils.getAttribute <ASN1Element>(field);
                    if (fieldInfo != null && fieldInfo.HasTag)
                    {
                        elementInfo.ASN1ElementInfo.HasTag        = true;
                        elementInfo.ASN1ElementInfo.TagClass      = fieldInfo.TagClass;
                        elementInfo.ASN1ElementInfo.IsImplicitTag = fieldInfo.IsImplicitTag;
                        elementInfo.ASN1ElementInfo.Tag           = fieldInfo.Tag;
                    }
                }
            }
            elementInfo.AnnotatedClass = field;
            //elementInfo.setGenericInfo(field.getGenericType());

            DecodedObject <object> val = null;

            if (CoderUtils.isAttributePresent <ASN1Null>(field))
            {
                val = decodeNull(decodedTag, field.PropertyType, elementInfo, stream);
            }
            else
            {
                val = decodeClassType(decodedTag, field.PropertyType, elementInfo, stream);
                if (val != null)
                {
                    result.Size = val.Size;
                    invokeSetterMethodForField(field, resultObj, val.Value, elementInfo);
                }
                else
                {
                    result = null;
                }
            }
            return(result);
        }
コード例 #7
0
        public virtual DecodedObject <object> decodeEnum(DecodedObject <object> decodedTag, System.Type objectClass, ElementInfo elementInfo, System.IO.Stream stream)
        {
            Type enumClass = null;

            foreach (MemberInfo member in objectClass.GetMembers())
            {
                if (member is System.Type)
                {
                    Type cls = (Type)member;
                    if (cls.IsEnum)
                    {
                        enumClass = cls;
                        break;
                    }
                }
            }
            ;

            System.Reflection.PropertyInfo field = objectClass.GetProperty("Value");


            DecodedObject <object> itemValue = decodeEnumItem(decodedTag, field.PropertyType, enumClass, elementInfo, stream);

            System.Reflection.FieldInfo param = null;
            if (itemValue != null)
            {
                object result = createInstanceForElement(objectClass, elementInfo);

                foreach (FieldInfo enumItem in enumClass.GetFields())
                {
                    if (CoderUtils.isAttributePresent <ASN1EnumItem>(enumItem))
                    {
                        ASN1EnumItem meta =
                            CoderUtils.getAttribute <ASN1EnumItem>(enumItem);
                        if (meta.Tag.Equals(itemValue.Value))
                        {
                            param = enumItem;
                            break;
                        }
                    }
                }
                invokeSetterMethodForField(field, result, param.GetValue(null), null);
                return(new DecodedObject <object>(result, itemValue.Size));
            }
            else
            {
                return(null);
            }
        }
コード例 #8
0
        /// <param name="obj">The encoded sequence</param>
        /// <param name="fieldIdx">Index of the sequence field to be encoded</param>
        /// <param name="field">Field of the encoded sequence to be encoded</param>
        /// <param name="stream"></param>
        /// <param name="elementInfo">ElementInfo for the encoded sequence</param>
        public virtual int encodeSequenceField(object obj, int fieldIdx, PropertyInfo field, System.IO.Stream stream, ElementInfo elementInfo)
        {
            ElementInfo info = new ElementInfo();

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

            if (CoderUtils.isNullField(field, info))
            {
                return(encodeNull(obj, stream, elementInfo));
            }
            else
            {
                object invokeObjResult = invokeGetterMethodForField(field, obj, info);
                if (invokeObjResult == null)
                {
                    CoderUtils.checkForOptionalField(field, info);
                    return(0);
                }
                else if (CoderUtils.isDefaultField(field, info))
                {
                    // skip the field if the current value equals to the default value (this is optional for BER, but mandatory for DER)
                    object newSequenceInstance = Activator.CreateInstance(obj.GetType());
                    CoderUtils.initDefaultValues(newSequenceInstance);
                    object defaultFieldValue = invokeGetterMethodForField(field, newSequenceInstance, info);
                    return(CoderUtils.AreEqual(defaultFieldValue, invokeObjResult) ? 0 : encodeClassType(invokeObjResult, stream, info));
                }
                else
                {
                    return(encodeClassType(invokeObjResult, stream, info));
                }
            }
        }
コード例 #9
0
        public virtual DecodedObject <object> decodeChoice(DecodedObject <object> decodedTag, System.Type objectClass, ElementInfo elementInfo, System.IO.Stream stream)
        {
            object choice = createInstanceForElement(objectClass, elementInfo);
            DecodedObject <object> val = null;

            PropertyInfo[] fields   = elementInfo.getProperties(objectClass);
            int            fieldIdx = 0;

            foreach (PropertyInfo field in fields)
            {
                ElementInfo info = new ElementInfo();
                info.AnnotatedClass = field;
                if (elementInfo.hasPreparedInfo())
                {
                    info.PreparedInfo = elementInfo.PreparedInfo.getPropertyMetadata(fieldIdx);
                }
                else
                {
                    info.ASN1ElementInfo = CoderUtils.getAttribute <ASN1Element>(field);
                }

                val = decodeClassType(decodedTag, field.PropertyType, info, stream);
                fieldIdx++;
                if (val != null)
                {
                    invokeSelectMethodForField(field, choice, val.Value, info);
                    break;
                }
                ;
            }
            if (val == null && !CoderUtils.isOptional(elementInfo))
            {
                throw new System.ArgumentException("The choice '" + objectClass.ToString() + "' does not have a selected item!");
            }
            else
            {
                return(new DecodedObject <object>(choice, val != null?val.Size:0));
            }
        }
コード例 #10
0
        protected ElementInfo getChoiceSelectedElement(Object obj, ElementInfo elementInfo)
        {
            ElementInfo info = null;

            PropertyInfo[] fields = elementInfo.getProperties(obj.GetType());

            int fieldIdx = 0;

            foreach (PropertyInfo field in fields)
            {
                info = new ElementInfo();
                info.AnnotatedClass = field;
                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 System.ArgumentException("The choice '" + obj.ToString() + "' does not have a selected item!");
            }
            return(info);
        }
コード例 #11
0
        public static SortedList <int, PropertyInfo> getSetOrder(Type objClass)
        {
            SortedList <int, PropertyInfo> fieldOrder = new SortedList <int, PropertyInfo>();
            int tagNA = -1;

            foreach (PropertyInfo field in objClass.GetProperties())
            {
                ASN1Element element = CoderUtils.getAttribute <ASN1Element>(field);
                if (element != null)
                {
                    try
                    {
                        if (element.HasTag)
                        {
                            fieldOrder.Add(element.Tag, field);
                        }
                        else
                        {
                            fieldOrder.Add(tagNA--, field);
                        }
                    }
                    catch (ArgumentException ex)
                    {
                        if (element.HasTag)
                        {
                            throw new Exception("Duplicate tag [" + element.Tag + "] for set has been detected!");
                        }
                        else
                        {
                            throw new Exception("Cannot be specified more field in SET without tag specified");
                        }
                    }
                }
            }
            return(fieldOrder);
        }
コード例 #12
0
        public virtual DecodedObject <object> decodeSequenceField(DecodedObject <object> fieldTag, object sequenceObj, int fieldIdx, PropertyInfo field, System.IO.Stream stream, ElementInfo elementInfo, bool checkOptional)
        {
            ElementInfo info = new ElementInfo();

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

            if (CoderUtils.isNullField(field, info))
            {
                return(decodeNull(fieldTag, field.PropertyType, info, stream));
            }
            else
            {
                DecodedObject <object> val = decodeClassType(fieldTag, field.PropertyType, info, stream);
                if (val != null)
                {
                    invokeSetterMethodForField(field, sequenceObj, val.Value, info);
                }
                else
                {
                    if (checkOptional)
                    {
                        CoderUtils.checkForOptionalField(field, info);
                    }
                }
                return(val);
            }
        }
コード例 #13
0
        /*public ASN1PreparedElementData(Type parentClass, String propertyName)
         * {
         *  try
         *  {
         *      PropertyInfo field = parentClass.GetProperty(propertyName);
         *      setupMetadata(field, field.PropertyType);
         *      setupAccessors(parentClass, field);
         *  }
         *  catch (Exception ex)
         *  {
         *      ex = null;
         *  }
         * }*/


        private void setupMetadata(ICustomAttributeProvider annotated, Type objectClass)
        {
            if (CoderUtils.isAttributePresent <ASN1_MMSDataArray>(annotated)) // Pavel
            {
                if (CoderUtils.isAttributePresent <ASN1SequenceOf>(annotated) && ASN1_MMSDataArray.Depth < 10)
                {
                    ASN1_MMSDataArray.Depth++;
                    typeMeta = new ASN1SequenceOfMetadata(CoderUtils.getAttribute <ASN1SequenceOf> (annotated),
                                                          objectClass,
                                                          annotated
                                                          );
                }
            }
            else
            if (CoderUtils.isAttributePresent <ASN1_MMSDataStructure>(annotated))    // Pavel
            {
                if (CoderUtils.isAttributePresent <ASN1SequenceOf>(annotated) && ASN1_MMSDataStructure.Depth < 10)
                {
                    ASN1_MMSDataStructure.Depth++;
                    typeMeta = new ASN1SequenceOfMetadata(CoderUtils.getAttribute <ASN1SequenceOf> (annotated),
                                                          objectClass,
                                                          annotated
                                                          );
                }
            }
            else
            if (CoderUtils.isAttributePresent <ASN1SequenceOf>(annotated))
            {
                typeMeta = new ASN1SequenceOfMetadata(CoderUtils.getAttribute <ASN1SequenceOf> (annotated),
                                                      objectClass,
                                                      annotated
                                                      );
            }
            else
            if (CoderUtils.isAttributePresent <ASN1Sequence>(annotated))
            {
                typeMeta = new ASN1SequenceMetadata(CoderUtils.getAttribute <ASN1Sequence> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1Choice>(annotated))
            {
                typeMeta = new ASN1ChoiceMetadata(CoderUtils.getAttribute <ASN1Choice> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1Enum>(annotated))
            {
                typeMeta = new ASN1EnumMetadata(CoderUtils.getAttribute <ASN1Enum> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1Boolean>(annotated))
            {
                typeMeta = new ASN1BooleanMetadata(CoderUtils.getAttribute <ASN1Boolean> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1Any>(annotated))
            {
                typeMeta = new ASN1AnyMetadata(CoderUtils.getAttribute <ASN1Any> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1Integer>(annotated))
            {
                typeMeta = new ASN1IntegerMetadata(CoderUtils.getAttribute <ASN1Integer> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1Real>(annotated))
            {
                typeMeta = new ASN1RealMetadata(CoderUtils.getAttribute <ASN1Real> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1OctetString>(annotated))
            {
                typeMeta = new ASN1OctetStringMetadata(CoderUtils.getAttribute <ASN1OctetString> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1BitString>(annotated) || objectClass.Equals(typeof(BitString)))
            {
                typeMeta = new ASN1BitStringMetadata(CoderUtils.getAttribute <ASN1BitString> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1ObjectIdentifier>(annotated) || objectClass.Equals(typeof(ObjectIdentifier)))
            {
                typeMeta = new ASN1ObjectIdentifierMetadata(CoderUtils.getAttribute <ASN1ObjectIdentifier>(annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1String>(annotated))
            {
                typeMeta = new ASN1StringMetadata(CoderUtils.getAttribute <ASN1String> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1Null>(annotated))
            {
                typeMeta = new ASN1NullMetadata(CoderUtils.getAttribute <ASN1Null> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1BoxedType>(annotated))
            {
                typeMeta = new ASN1BoxedTypeMetadata(objectClass, CoderUtils.getAttribute <ASN1BoxedType> (annotated));
            }
            else
            if (CoderUtils.isAttributePresent <ASN1Element>(annotated))
            {
                typeMeta = new ASN1ElementMetadata(CoderUtils.getAttribute <ASN1Element> (annotated));
            }
            else
            if (objectClass.Equals(typeof(String)))
            {
                typeMeta = new ASN1StringMetadata( );
            }
            else
            if (objectClass.Equals(typeof(int)))
            {
                typeMeta = new ASN1IntegerMetadata( );
            }
            else
            if (objectClass.Equals(typeof(long)))
            {
                typeMeta = new ASN1IntegerMetadata( );
            }
            else
            if (objectClass.Equals(typeof(double)))
            {
                typeMeta = new ASN1RealMetadata( );
            }
            else
            if (objectClass.Equals(typeof(bool)))
            {
                typeMeta = new ASN1BooleanMetadata( );
            }
            else
            if (objectClass.Equals(typeof(byte[])))
            {
                typeMeta = new ASN1OctetStringMetadata( );
            }

            if (CoderUtils.isAttributePresent <ASN1Element>(annotated))
            {
                asn1ElementInfo = new ASN1ElementMetadata(CoderUtils.getAttribute <ASN1Element>(annotated));
            }

            //ASN1CyclicDefinition.Depth = 0; // Pavel
            setupConstraint(annotated);
        }
コード例 #14
0
 public T getParentAttribute <T>()
 {
     return(CoderUtils.getAttribute <T>(parentAnnotatedClass));
 }
コード例 #15
0
 public T getAttribute <T>()
 {
     return(CoderUtils.getAttribute <T>(annotatedClass));
 }
コード例 #16
0
        public virtual DecodedObject <object> decodeSequence(DecodedObject <object> decodedTag, System.Type objectClass, ElementInfo elementInfo, System.IO.Stream stream)
        {
            object sequence = createInstanceForElement(objectClass, elementInfo);

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

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

            for (int i = 0; i < fields.Length; i++)
            {
                PropertyInfo           field = fields[i];
                DecodedObject <object> obj   = decodeSequenceField(
                    fieldTag, sequence, i, field, stream, elementInfo, true
                    );
                if (obj != null)
                {
                    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;
                            }
                        }
                    }
                }
                ;
            }
            return(new DecodedObject <object>(sequence, sizeOfSequence));
        }