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 override int encodeInteger(object obj, System.IO.Stream stream, ElementInfo elementInfo) { int result = 0; bool hasConstraint = false; long min = 0, max = 0; if (elementInfo.hasPreparedInfo()) { if (elementInfo.PreparedInfo.hasConstraint() && elementInfo.PreparedInfo.Constraint is ASN1ValueRangeConstraintMetadata) { IASN1ConstraintMetadata constraint = elementInfo.PreparedInfo.Constraint; hasConstraint = true; min = ((ASN1ValueRangeConstraintMetadata)constraint).Min; max = ((ASN1ValueRangeConstraintMetadata)constraint).Max; } } else if (elementInfo.isAttributePresent <ASN1ValueRangeConstraint>()) { hasConstraint = true; ASN1ValueRangeConstraint constraint = elementInfo.getAttribute <ASN1ValueRangeConstraint>(); min = constraint.Min; max = constraint.Max; } if (obj.GetType().Equals(typeof(int))) { int val = (int)obj; BitArrayOutputStream bitStream = (BitArrayOutputStream)stream; if (hasConstraint) { result += encodeConstraintNumber(val, min, max, bitStream); } else { result += encodeUnconstraintNumber(val, bitStream); } } else { long val = (long)obj; BitArrayOutputStream bitStream = (BitArrayOutputStream)stream; if (hasConstraint) { result += encodeConstraintNumber(val, min, max, bitStream); } else { result += encodeUnconstraintNumber(val, bitStream); } } return(result); }
protected int encodeLength(int val, ElementInfo elementInfo, System.IO.Stream stream) { CoderUtils.checkConstraints(val, elementInfo); int resultSize = 0; BitArrayOutputStream bitStream = (BitArrayOutputStream)stream; if (elementInfo.hasPreparedInfo()) { if (elementInfo.PreparedInfo.hasConstraint()) { IASN1ConstraintMetadata constraint = elementInfo.PreparedInfo.Constraint; if (constraint is ASN1ValueRangeConstraintMetadata) { resultSize += encodeConstraintLengthDeterminant( val, (int)((ASN1ValueRangeConstraintMetadata)constraint).Min, (int)((ASN1ValueRangeConstraintMetadata)constraint).Max, bitStream ); } else if (constraint is ASN1SizeConstraintMetadata) { } } else { resultSize += encodeLengthDeterminant(val, bitStream); } } else { if (elementInfo.isAttributePresent <ASN1ValueRangeConstraint>()) { ASN1ValueRangeConstraint constraint = elementInfo.getAttribute <ASN1ValueRangeConstraint>(); resultSize += encodeConstraintLengthDeterminant(val, (int)constraint.Min, (int)constraint.Max, bitStream); } else if (elementInfo.isAttributePresent <ASN1SizeConstraint>()) { ASN1SizeConstraint constraint = elementInfo.getAttribute <ASN1SizeConstraint>(); } else { resultSize += encodeLengthDeterminant(val, bitStream); } } return(resultSize); }
protected virtual int decodeLength(ElementInfo elementInfo, System.IO.Stream stream) { int result = 0; BitArrayInputStream bitStream = (BitArrayInputStream)stream; if (elementInfo.hasPreparedInfo()) { if (elementInfo.PreparedInfo.hasConstraint()) { IASN1ConstraintMetadata constraint = elementInfo.PreparedInfo.Constraint; if (constraint is ASN1ValueRangeConstraintMetadata) { result = decodeConstraintLengthDeterminant( (int)((ASN1ValueRangeConstraintMetadata)constraint).Min, (int)((ASN1ValueRangeConstraintMetadata)constraint).Max, bitStream ); } else if (constraint is ASN1SizeConstraintMetadata) { result = (int)((ASN1SizeConstraintMetadata)constraint).Max; } } else { result = decodeLengthDeterminant(bitStream); } } else if (elementInfo.isAttributePresent <ASN1ValueRangeConstraint>()) { ASN1ValueRangeConstraint constraint = elementInfo.getAttribute <ASN1ValueRangeConstraint>(); result = decodeConstraintLengthDeterminant((int)constraint.Min, (int)constraint.Max, bitStream); } else if (elementInfo.isAttributePresent <ASN1SizeConstraint>()) { ASN1SizeConstraint constraint = elementInfo.getAttribute <ASN1SizeConstraint>(); result = (int)constraint.Max; } else { result = decodeLengthDeterminant(bitStream); } CoderUtils.checkConstraints(result, elementInfo); return(result); }
protected virtual int decodeStringLength2(ElementInfo elementInfo, Stream stream) { int resultSize = 0; BitArrayInputStream bitStream = (BitArrayInputStream)stream; if (elementInfo.isAttributePresent <ASN1ValueRangeConstraint>()) { ASN1ValueRangeConstraint constraint = elementInfo.getAttribute <ASN1ValueRangeConstraint>(); resultSize = decodeConstraintLengthDeterminant((int)constraint.Min, (int)constraint.Max, bitStream); } else { resultSize = decodeLengthDeterminant(bitStream); } return(resultSize); }
public override DecodedObject <object> decodeInteger(DecodedObject <object> decodedTag, System.Type objectClass, ElementInfo elementInfo, System.IO.Stream stream) { bool hasConstraint = false; long min = 0, max = 0; if (elementInfo.hasPreparedInfo()) { if (elementInfo.PreparedInfo.hasConstraint() && elementInfo.PreparedInfo.Constraint is ASN1ValueRangeConstraintMetadata) { IASN1ConstraintMetadata constraint = elementInfo.PreparedInfo.Constraint; hasConstraint = true; min = ((ASN1ValueRangeConstraintMetadata)constraint).Min; max = ((ASN1ValueRangeConstraintMetadata)constraint).Max; } } else if (elementInfo.isAttributePresent <ASN1ValueRangeConstraint>()) { hasConstraint = true; ASN1ValueRangeConstraint constraint = elementInfo.getAttribute <ASN1ValueRangeConstraint>(); min = constraint.Min; max = constraint.Max; } DecodedObject <object> result = new DecodedObject <object>(); BitArrayInputStream bitStream = (BitArrayInputStream)stream; int val = 0; if (hasConstraint) { ASN1ValueRangeConstraint constraint = elementInfo.getAttribute <ASN1ValueRangeConstraint>(); val = (int)decodeConstraintNumber(min, max, bitStream); } else { val = decodeUnconstraintNumber(bitStream); } result.Value = val; return(result); }
public ASN1ValueRangeConstraintMetadata(ASN1ValueRangeConstraint annotation) { this.minValue = annotation.Min; this.maxValue = annotation.Max; }