diff options
Diffstat (limited to 'crypto/src/asn1/BERBitString.cs')
-rw-r--r-- | crypto/src/asn1/BERBitString.cs | 72 |
1 files changed, 36 insertions, 36 deletions
diff --git a/crypto/src/asn1/BERBitString.cs b/crypto/src/asn1/BERBitString.cs index 156ea2d1f..a012d2a7c 100644 --- a/crypto/src/asn1/BERBitString.cs +++ b/crypto/src/asn1/BERBitString.cs @@ -1,8 +1,6 @@ using System; using System.Diagnostics; -using Org.BouncyCastle.Utilities; - namespace Org.BouncyCastle.Asn1 { public class BerBitString @@ -113,54 +111,56 @@ namespace Org.BouncyCastle.Asn1 this.segmentLimit = DefaultSegmentLimit; } - internal override bool EncodeConstructed() + internal override bool EncodeConstructed(int encoding) { - // NOTE: Assumes BER encoding + if (Asn1OutputStream.EncodingBer != encoding) + return base.EncodeConstructed(encoding); + return null != elements || contents.Length > segmentLimit; } - internal override int EncodedLength(bool withID) + internal override int EncodedLength(int encoding, bool withID) { - throw Platform.CreateNotImplementedException("BerBitString.EncodedLength"); - - // TODO This depends on knowing it's not DER - //if (!EncodeConstructed()) - // return EncodedLength(withID, contents.Length); - - //int totalLength = withID ? 4 : 3; - - //if (null != elements) - //{ - // for (int i = 0; i < elements.Length; ++i) - // { - // totalLength += elements[i].EncodedLength(true); - // } - //} - //else if (contents.Length < 2) - //{ - // // No bits - //} - //else - //{ - // int extraSegments = (contents.Length - 2) / (segmentLimit - 1); - // totalLength += extraSegments * EncodedLength(true, segmentLimit); - - // int lastSegmentLength = contents.Length - (extraSegments * (segmentLimit - 1)); - // totalLength += EncodedLength(true, lastSegmentLength); - //} - - //return totalLength; + if (Asn1OutputStream.EncodingBer != encoding) + return base.EncodedLength(encoding, withID); + + if (!EncodeConstructed(encoding)) + return EncodedLength(withID, contents.Length); + + int totalLength = withID ? 4 : 3; + + if (null != elements) + { + for (int i = 0; i < elements.Length; ++i) + { + totalLength += elements[i].EncodedLength(encoding, true); + } + } + else if (contents.Length < 2) + { + // No bits + } + else + { + int extraSegments = (contents.Length - 2) / (segmentLimit - 1); + totalLength += extraSegments * EncodedLength(true, segmentLimit); + + int lastSegmentLength = contents.Length - (extraSegments * (segmentLimit - 1)); + totalLength += EncodedLength(true, lastSegmentLength); + } + + return totalLength; } internal override void Encode(Asn1OutputStream asn1Out, bool withID) { - if (!asn1Out.IsBer) + if (Asn1OutputStream.EncodingBer != asn1Out.Encoding) { base.Encode(asn1Out, withID); return; } - if (!EncodeConstructed()) + if (!EncodeConstructed(asn1Out.Encoding)) { Encode(asn1Out, withID, contents, 0, contents.Length); return; |