Repository: dotnet/aspnetcore
File: src/DataProtection/DataProtection/src/KeyManagement/KeyRingBasedDataProtector.cs (C#)

Code before:
            // index 0: magic header
            BinaryPrimitives.WriteUInt32BigEndian(targetSpan.Slice(0), MAGIC_HEADER_V0);
            // index 4: key (skipped for now, will be populated in `GetAadForKey()`)
            // index 4 + keySize: purposeCount
            BinaryPrimitives.WriteInt32BigEndian(targetSpan.Slice(4 + keySize), purposes.Length);

            int index = 4 + keySize + 4; // starting from first purpose
            for (int i = 0; i < purposes.Length; i++)
            {
                string purpose = purposes[i];

                // writing `utf8ByteCount (7-bit encoded integer) || utf8Text`
                // we have already calculated the lengths of the purpose strings, so just get it from the pool
                index += Write7BitEncodedInt(purposeLengthsPool[i], targetSpan.Slice(index));
                index += EncodingUtil.SecureUtf8Encoding.GetBytes(purpose.AsSpan(), targetSpan.Slice(index));
            }

            ArrayPool<int>.Shared.Return(purposeLengthsPool);
            Debug.Assert(index == targetArr.Length);

            _aadTemplate = targetArr;
        }

        private static int Measure7BitEncodedUIntLength(uint value)
        {
            return ((31 - System.Numerics.BitOperations.LeadingZeroCount(value | 1)) / 7) + 1;

            // does the same as the following code:
            // int count = 1;
            // while ((value >>= 7) != 0)
            // {
            //     count++;
            // }
            // return count;
        }

        private static int Write7BitEncodedInt(int value, Span<byte> target)
        {
            uint uValue = (uint)value;

            // Write out an int 7 bits at a time. The high bit of the byte,
            // when on, tells reader to continue reading more bytes.
            //
            // Using the constants 0x7F and ~0x7F below offers smaller
            // codegen than using the constant 0x80.

            int index = 0;
            while (uValue > 0x7Fu)
            {
                target[index++] = (byte)(uValue | ~0x7Fu);
                uValue >>= 7;

Review comment (suggestion):
I know this is my code - however, I'd like to see a test on this specifically, in particular checking all the 7-bit boundaries, i.e. a `[Theory]` with tests for:

- 0 => 1
- 1 => 1
- 0b0_1111111 => 1
- 0b1_0000000 => 2
- 0b1111111_1111111 => 2
- 0b1_0000000_0000000 => 3
- 0b1111111_1111111_1111111 => 3
- 0b1_0000000_0000000_0000000 => 4
- 0b1111111_1111111_1111111_1111111 => 4
- 0b1_0000000_0000000_0000000_0000000 => 5
- uint.MaxValue =>5

Code after:
                var encoded7BitUIntLength = purposeLength.Measure7BitEncodedUIntLength();
                totalPurposeLen += purposeLength /* length of actual string */ + encoded7BitUIntLength /* length of 'string length' 7-bit encoded int */;
            }

            byte[] targetArr = new byte[totalPurposeLen];
            var targetSpan = targetArr.AsSpan();

            // index 0: magic header
            BinaryPrimitives.WriteUInt32BigEndian(targetSpan.Slice(0), MAGIC_HEADER_V0);
            // index 4: key (skipped for now, will be populated in `GetAadForKey()`)
            // index 4 + keySize: purposeCount
            BinaryPrimitives.WriteInt32BigEndian(targetSpan.Slice(4 + keySize), targetLength);

            int index = 4 /* MAGIC_HEADER_V0 */ + keySize + 4 /* purposeLength */; // starting from first purpose
            for (int i = 0; i < targetLength; i++)
            {
                string purpose = purposes[i];

                // writing `utf8ByteCount (7-bit encoded integer)`
                // we have already calculated the lengths of the purpose strings, so just get it from the pool
                index += targetSpan.Slice(index).Write7BitEncodedInt(purposeLengthsPool[i]);

                // write the utf8text for the purpose
                index += EncodingUtil.SecureUtf8Encoding.GetBytes(purpose, charIndex: 0, charCount: purpose.Length, bytes: targetArr, byteIndex: index);
            }

            if (lease is not null)
            {
                ArrayPool<int>.Shared.Return(lease);
            }
            Debug.Assert(index == targetArr.Length);

            return targetArr;
        }
    }

    private enum UnprotectStatus
    {
        Ok,
        DefaultEncryptionKeyChanged,
        DecryptionKeyWasRevoked
    }
}