/// <summary> /// Decodes the object by PER. /// </summary> /// <param name="buffer">A buffer that contains a PER encoding result.</param> /// <param name="aligned">Indicating whether the PER decoding is aligned.</param> /// <remarks>Length is included.</remarks> protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true) { ByteArrayValue = Asn1StandardProcedure.PerDecodeArray <byte>(buffer, decodingBuffer => { Asn1Integer ai = new Asn1Integer(null, 0, 15); ai.PerDecode(buffer); if (Constraint == null || Constraint.PermittedCharSet == null) { if (ai.Value == 0) { return((byte)' '); } else if (ai.Value >= 1 && ai.Value <= 10) { return((byte)((byte)'0' + ai.Value - 1)); } else { throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + "Only 0~9 and SPACE are allowed in NumericString."); } } else { int index = (int)ai.Value; return((byte)CharSetInArray[(int)ai.Value]); } }, Constraint != null && Constraint.HasMinSize ? Constraint.MinSize : (long?)null, Constraint != null && Constraint.HasMaxSize ? Constraint.MaxSize : (long?)null, true); }
/// <summary> /// Decodes the object by PER. /// </summary> /// <param name="buffer">A buffer that contains a PER encoding result.</param> /// <param name="aligned">Indicating whether the PER decoding is aligned.</param> /// <remarks>Length is included.</remarks> protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true) { if (Constraint != null && Constraint.HasMinSize && Constraint.HasMinSize && Constraint.MinSize == Constraint.MaxSize && Constraint.MinSize <= 2) //Ref. X.691:16.6 { long minSize = Constraint.MinSize; bool[] bitResult = buffer.ReadBits((int)(8 * minSize)); byte[] byteResult = new byte[(int)minSize]; //Convert bool array to byte array int byteIndex = 0, bitIndex = 0; foreach (bool b in bitResult) { if (b) { byteResult[byteIndex] |= (byte)(1 << (7 - bitIndex)); } bitIndex++; if (bitIndex == 8) { bitIndex = 0; byteIndex++; } } ByteArrayValue = byteResult; } else { ByteArrayValue = Asn1StandardProcedure.PerDecodeArray(buffer, decodingBuffer => decodingBuffer.ReadByte(), Constraint != null && Constraint.HasMinSize ? Constraint.MinSize : (long?)null, Constraint != null && Constraint.HasMaxSize ? Constraint.MaxSize : (long?)null); } }