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)); }
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 + "' 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 + "' out of bound"); } } else if (elementInfo.isAttributePresent <ASN1SizeConstraint>()) { ASN1SizeConstraint constraint = elementInfo.getAttribute <ASN1SizeConstraint>(); if (val != constraint.Max) { throw new Exception("Length of '" + elementInfo.AnnotatedClass + "' out of bound"); } } } }
public virtual int encodeSequenceField(object obj, int fieldIdx, PropertyInfo field, 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); }
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)); } }
public void invokeSelectMethodForField(PropertyInfo field, object obj, object param, ElementInfo info) { if (info.hasPreparedInfo()) { info.PreparedInfo.invokeDoSelectMethod(obj, param); } else { MethodInfo method = CoderUtils.findDoSelectMethodForField(field, obj.GetType()); method.Invoke(obj, new[] { param }); } }
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); }
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); }
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); }
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); }
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); }
public static bool isOptionalField(ICustomAttributeProvider field, ElementInfo elementInfo) { if (elementInfo.hasPreparedInfo()) { if (elementInfo.hasPreparedASN1ElementInfo()) { return(elementInfo.PreparedASN1ElementInfo.IsOptional || elementInfo.PreparedASN1ElementInfo.HasDefaultValue); } return(false); } else if (isAttributePresent <ASN1Element>(field)) { ASN1Element info = getAttribute <ASN1Element>(field); if (info.IsOptional || info.HasDefaultValue) { return(true); } } return(false); }
public virtual DecodedObject <object> decodeChoice(DecodedObject <object> decodedTag, Type objectClass, ElementInfo elementInfo, 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 ArgumentException("The choice '" + objectClass + "' does not have a selected item!"); } else { return(new DecodedObject <object>(choice, val != null ? val.Size : 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 ArgumentException("The choice '" + obj + "' does not have a selected item!"); } return(info); }
public virtual DecodedObject <object> decodeSequenceField(DecodedObject <object> fieldTag, object sequenceObj, int fieldIdx, PropertyInfo field, 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); } }
public virtual int encodeClassType(object obj, 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); }
public virtual DecodedObject <object> decodeClassType(DecodedObject <object> decodedTag, Type objectClass, ElementInfo elementInfo, Stream stream) { if (CoderUtils.isImplements(objectClass, typeof(IASN1PreparedElement))) { return(decodePreparedElement(decodedTag, objectClass, elementInfo, stream)); } else if (elementInfo.hasPreparedInfo()) { 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)); } }
public virtual DecodedObject <object> decodeSequence(DecodedObject <object> decodedTag, Type objectClass, ElementInfo elementInfo, 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)); }