Exemple #1
0
        public virtual object invokeGetterMethodForField(PropertyInfo field, object obj, ElementInfo info)
        {
            MethodInfo method = null;

            if (info != null && info.hasPreparedInfo())
            {
                method = info.PreparedInfo.IsPresentMethod;
            }
            else
            {
                method = CoderUtils.findIsPresentMethodForField(field, obj.GetType());
            }
            if (method != null)
            {
                if ((bool)method.Invoke(obj, null))
                {
                    return(field.GetValue(obj, null));
                }
                else
                {
                    return(null);
                }
            }
            return(field.GetValue(obj, null));
        }
Exemple #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);
        }
Exemple #3
0
 public static void checkConstraints(long val, ElementInfo elementInfo)
 {
     if (elementInfo.hasPreparedInfo())
     {
         if (elementInfo.PreparedInfo.hasConstraint())
         {
             if (!elementInfo.PreparedInfo.Constraint.checkValue(val))
             {
                 throw new Exception("Length of '" + elementInfo.AnnotatedClass.ToString() + "' out of bound");
             }
         }
     }
     else
     {
         if (elementInfo.isAttributePresent <ASN1ValueRangeConstraint>())
         {
             ASN1ValueRangeConstraint constraint = elementInfo.getAttribute <ASN1ValueRangeConstraint>();
             if (val > constraint.Max || val < constraint.Min)
             {
                 throw new Exception("Length of '" + elementInfo.AnnotatedClass.ToString() + "' out of bound");
             }
         }
         else
         if (elementInfo.isAttributePresent <ASN1SizeConstraint>())
         {
             ASN1SizeConstraint constraint = elementInfo.getAttribute <ASN1SizeConstraint>();
             if (val != constraint.Max)
             {
                 throw new Exception("Length of '" + elementInfo.AnnotatedClass.ToString() + "' out of bound");
             }
         }
     }
 }
Exemple #4
0
 public void invokeSelectMethodForField(PropertyInfo field, object obj, object param, ElementInfo info)
 {
     if (info.hasPreparedInfo())
     {
         info.PreparedInfo.invokeDoSelectMethod(obj, param);
     }
     else
     {
         CoderUtils.findDoSelectMethodForField(field, obj.GetType()).Invoke(obj, new object[] { param });
     }
 }
Exemple #5
0
 public virtual bool invokeSelectedMethodForField(PropertyInfo field, object obj, ElementInfo info)
 {
     if (info != null && info.hasPreparedInfo())
     {
         return((bool)info.PreparedInfo.invokeIsSelectedMethod(obj, null));
     }
     else
     {
         MethodInfo method = CoderUtils.findIsSelectedMethodForField(field, obj.GetType());
         return((bool)method.Invoke(obj, null));
     }
 }
Exemple #6
0
        public static bool isAnyField(ICustomAttributeProvider field, ElementInfo elementInfo)
        {
            bool isAny = false;

            if (elementInfo.hasPreparedInfo())
            {
                isAny = elementInfo.PreparedInfo.TypeMetadata is ASN1AnyMetadata;
            }
            else
            {
                isAny = isAttributePresent <ASN1Any>(field);//. isAnnotationPresent(.class);
            }
            return(isAny);
        }
Exemple #7
0
        public static bool isNullField(ICustomAttributeProvider field, ElementInfo elementInfo)
        {
            bool isNull = false;

            if (elementInfo.hasPreparedInfo())
            {
                isNull = elementInfo.PreparedInfo.TypeMetadata is ASN1NullMetadata;
            }
            else
            {
                isNull = isAttributePresent <ASN1Null>(field);
            }
            return(isNull);
        }
Exemple #8
0
        public static bool isOptional(ElementInfo elementInfo)
        {
            bool result = false;

            if (elementInfo.hasPreparedInfo())
            {
                result = elementInfo.PreparedASN1ElementInfo.IsOptional ||
                         elementInfo.PreparedASN1ElementInfo.HasDefaultValue;
            }
            else
            {
                result = elementInfo.ASN1ElementInfo != null && elementInfo.ASN1ElementInfo.IsOptional;
            }
            return(result);
        }
Exemple #9
0
        public static bool isSequenceSetOf(ElementInfo elementInfo)
        {
            bool isEqual = false;

            if (elementInfo.hasPreparedInfo())
            {
                isEqual = ((ASN1SequenceOfMetadata)elementInfo.PreparedInfo.TypeMetadata).IsSetOf;
            }
            else
            {
                ASN1SequenceOf seq = getAttribute <ASN1SequenceOf>(elementInfo.AnnotatedClass);
                isEqual = seq.IsSetOf;
            }
            return(isEqual);
        }
Exemple #10
0
 public static bool isDefaultField(ICustomAttributeProvider field, ElementInfo elementInfo)
 {
     if (elementInfo.hasPreparedInfo())
     {
         return(elementInfo.hasPreparedASN1ElementInfo() && elementInfo.PreparedASN1ElementInfo.HasDefaultValue);
     }
     else if (isAttributePresent <ASN1Element>(field))
     {
         return(getAttribute <ASN1Element>(field).HasDefaultValue);
     }
     else
     {
         return(false);
     }
 }
Exemple #11
0
 public static bool isOptionalField(ICustomAttributeProvider field, ElementInfo elementInfo)
 {
     if (elementInfo.hasPreparedInfo())
     {
         return(elementInfo.hasPreparedASN1ElementInfo() && (elementInfo.PreparedASN1ElementInfo.IsOptional || elementInfo.PreparedASN1ElementInfo.HasDefaultValue));
     }
     else if (isAttributePresent <ASN1Element>(field))
     {
         ASN1Element info = getAttribute <ASN1Element>(field);
         return(info.IsOptional || info.HasDefaultValue);
     }
     else
     {
         return(false);
     }
 }
Exemple #12
0
        public static int getStringTagForElement(ElementInfo elementInfo)
        {
            int result = UniversalTags.PrintableString;

            if (elementInfo.hasPreparedInfo())
            {
                result = ((ASN1StringMetadata)elementInfo.PreparedInfo.TypeMetadata).StringType;
            }
            else if (elementInfo.isAttributePresent <ASN1String>())
            {
                ASN1String val = elementInfo.getAttribute <ASN1String>();
                result = val.StringType;
            }
            else if (elementInfo.ParentAnnotatedClass != null && elementInfo.isParentAttributePresent <ASN1String>())
            {
                ASN1String value = elementInfo.getParentAttribute <ASN1String>();
                result = value.StringType;
            }
            return(result);
        }
Exemple #13
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));
                }
            }
        }
Exemple #14
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));
            }
        }
Exemple #15
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);
        }
Exemple #16
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);
            }
        }
Exemple #17
0
        public virtual int encodeClassType(object obj, System.IO.Stream stream, ElementInfo elementInfo)
        {
            int resultSize = 0;

            if (elementInfo.hasPreparedInfo())
            {
                resultSize += elementInfo.PreparedInfo.TypeMetadata.encode(this, obj, stream, elementInfo);
            }
            else
            if (obj is IASN1PreparedElement)
            {
                resultSize += encodePreparedElement(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1SequenceOf>())
            {
                resultSize += encodeSequenceOf(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1Sequence>())
            {
                resultSize += encodeSequence(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1Choice>())
            {
                resultSize += encodeChoice(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1BoxedType>())
            {
                resultSize += encodeBoxedType(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1Enum>())
            {
                resultSize += encodeEnum(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1Boolean>())
            {
                resultSize += encodeBoolean(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1Any>())
            {
                resultSize += encodeAny(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1Integer>())
            {
                resultSize += encodeInteger(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1Real>())
            {
                resultSize += encodeReal(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1OctetString>())
            {
                resultSize += encodeOctetString(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1BitString>() || obj.GetType().Equals(typeof(BitString)))
            {
                resultSize += encodeBitString(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1ObjectIdentifier>() || obj.GetType().Equals(typeof(ObjectIdentifier)))
            {
                resultSize += encodeObjectIdentifier(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1String>())
            {
                resultSize += encodeString(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1Null>())
            {
                resultSize += encodeNull(obj, stream, elementInfo);
            }
            else
            if (elementInfo.isAttributePresent <ASN1Element>())
            {
                resultSize += encodeElement(obj, stream, elementInfo);
            }
            else
            {
                resultSize += encodeCSElement(obj, stream, elementInfo);
            }
            return(resultSize);
        }
Exemple #18
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));
        }
Exemple #19
0
 public virtual DecodedObject <object> decodeClassType(DecodedObject <object> decodedTag, System.Type objectClass, ElementInfo elementInfo, System.IO.Stream stream)
 {
     if (CoderUtils.isImplements(objectClass, typeof(IASN1PreparedElement)))
     {
         return(decodePreparedElement(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.hasPreparedInfo() && elementInfo.PreparedInfo.TypeMetadata != null)     // Pavel
     {
         return(elementInfo.PreparedInfo.TypeMetadata.decode(
                    this, decodedTag, objectClass, elementInfo, stream
                    ));
     }
     else
     if (elementInfo.isAttributePresent <ASN1SequenceOf>())
     {
         return(decodeSequenceOf(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1Sequence>())
     {
         return(decodeSequence(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1Choice>())
     {
         return(decodeChoice(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1BoxedType>())
     {
         return(decodeBoxedType(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1Enum>())
     {
         return(decodeEnum(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1Boolean>())
     {
         return(decodeBoolean(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1Any>())
     {
         return(decodeAny(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1Integer>())
     {
         return(decodeInteger(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1Real>())
     {
         return(decodeReal(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1OctetString>())
     {
         return(decodeOctetString(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1BitString>() || elementInfo.AnnotatedClass.Equals(typeof(BitString)))
     {
         return(decodeBitString(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1ObjectIdentifier>() || elementInfo.AnnotatedClass.Equals(typeof(ObjectIdentifier)))
     {
         return(decodeObjectIdentifier(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1String>())
     {
         return(decodeString(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1Null>())
     {
         return(decodeNull(decodedTag, objectClass, elementInfo, stream));
     }
     else
     if (elementInfo.isAttributePresent <ASN1Element>())
     {
         return(decodeElement(decodedTag, objectClass, elementInfo, stream));
     }
     else
     {
         return(decodeCSElement(decodedTag, objectClass, elementInfo, stream));
     }
 }