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

Code before:
            var keySize = sizeof(Guid);
            int totalPurposeLen = 4 + keySize + 4;

            int[]? lease = null;
            Span<int> purposeLengthsPool = purposes.Length <= 32 ? stackalloc int[purposes.Length] : (lease = ArrayPool<int>.Shared.Rent(purposes.Length)).AsSpan(0, purposes.Length);
            for (int i = 0; i < purposes.Length; i++)
            {
                string purpose = purposes[i];

                int purposeLength = EncodingUtil.SecureUtf8Encoding.GetByteCount(purpose);
                purposeLengthsPool[i] = purposeLength;

                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), 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)`
                // 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,

Review comment (suggestion):
```suggestion
            int index = sizeof(uint) + keySize + sizeof(uint); // starting from first purpose
```

Or alternatively:

```suggestion
            int index = 4 /* MAGIC_HEADER_V0 */ + keySize + 4 /* purposeLength */; // starting from first purpose
```

Code after:
            var keySize = sizeof(Guid);
            int totalPurposeLen = 4 + keySize + 4;

            int[]? lease = null;
            var targetLength = purposes.Length;
            Span<int> purposeLengthsPool = targetLength <= 32 ? stackalloc int[targetLength] : (lease = ArrayPool<int>.Shared.Rent(targetLength)).AsSpan(0, targetLength);
            for (int i = 0; i < targetLength; i++)
            {
                string purpose = purposes[i];

                int purposeLength = EncodingUtil.SecureUtf8Encoding.GetByteCount(purpose);
                purposeLengthsPool[i] = purposeLength;

                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
    {