Exemple #1
0
        private static int DetermineCerBitStringTotalLength(Asn1Tag tag, int contentLength)
        {
            const int MaxCERSegmentSize = AsnReader.MaxCERSegmentSize;

            // Every segment has an "unused bit count" byte.
            const int MaxCERContentSize = MaxCERSegmentSize - 1;

            Debug.Assert(contentLength > MaxCERContentSize);

            int fullSegments = Math.DivRem(contentLength, MaxCERContentSize, out int lastContentSize);

            // The tag size is 1 byte.
            // The length will always be encoded as 82 03 E8 (3 bytes)
            // And 1000 content octets (by T-REC-X.690-201508 sec 9.2)
            const int FullSegmentEncodedSize = 1004;

            Debug.Assert(
                1 + 1 + MaxCERSegmentSize + GetEncodedLengthSubsequentByteCount(MaxCERSegmentSize) == FullSegmentEncodedSize);

            int remainingEncodedSize;

            if (lastContentSize == 0)
            {
                remainingEncodedSize = 0;
            }
            else
            {
                // One byte of tag, minimum one byte of length, and one byte of unused bit count.
                remainingEncodedSize = 3 + lastContentSize + GetEncodedLengthSubsequentByteCount(lastContentSize);
            }

            // Reduce the number of copies by pre-calculating the size.
            // +2 for End-Of-Contents
            // +1 for 0x80 indefinite length
            // +tag length
            return((fullSegments * FullSegmentEncodedSize) + remainingEncodedSize + 3 + tag.CalculateEncodedSize());
        }