< Summary

Information
Class: DeltaSharp.Format.DeltaChecksum
Assembly: DeltaSharp
File(s): /home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Format/DeltaChecksum.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 67
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Checksum(...)100%1010100%

File(s)

/home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Format/DeltaChecksum.cs

#LineLine coverage
 1using System.Buffers.Binary;
 2
 3namespace DeltaSharp.Format;
 4
 5
 6/// <summary>Computes the Fossil 32-bit additive checksum used by the built-in delta formats.</summary>
 7/// <remarks>This checksum is neither CRC-32 nor a cryptographic integrity or authenticity check.</remarks>
 8public class DeltaChecksum: IDeltaChecksum
 9{
 10
 11    /// <summary>Computes the Fossil additive checksum of the supplied bytes.</summary>
 12    /// <param name="arr">The target bytes to checksum.</param>
 13    /// <returns>The sum of big-endian 32-bit words modulo 2^32, with the final word padded with trailing zeros; zero fo
 14    public unsafe uint Checksum(ReadOnlySpan<byte> arr)
 15    {
 6629016        if (arr.Length == 0) return 0;
 17
 6225018        fixed (byte* ptr = arr)
 19        {
 6225020            uint sum = 0;
 6225021            int z = 0;
 22
 6225023            var limit = arr.Length - 32;
 453935024            while (z <= limit)
 25            {
 447710026                sum += BinaryPrimitives.ReverseEndianness(*(uint*)(ptr + z));
 447710027                sum += BinaryPrimitives.ReverseEndianness(*(uint*)(ptr + z + 4));
 447710028                sum += BinaryPrimitives.ReverseEndianness(*(uint*)(ptr + z + 8));
 447710029                sum += BinaryPrimitives.ReverseEndianness(*(uint*)(ptr + z + 12));
 447710030                sum += BinaryPrimitives.ReverseEndianness(*(uint*)(ptr + z + 16));
 447710031                sum += BinaryPrimitives.ReverseEndianness(*(uint*)(ptr + z + 20));
 447710032                sum += BinaryPrimitives.ReverseEndianness(*(uint*)(ptr + z + 24));
 447710033                sum += BinaryPrimitives.ReverseEndianness(*(uint*)(ptr + z + 28));
 34
 447710035                z += 32;
 36            }
 37
 6225038            limit = arr.Length - 4;
 27221039            while (z <= limit)
 40            {
 20996041                sum += BinaryPrimitives.ReverseEndianness(*(uint*)(ptr + z));
 20996042                z += 4;
 43            }
 44
 6225045            int rem = (arr.Length - z);
 46
 6225047            switch (rem & 3)
 48            {
 49                case 3:
 1493050                    sum += (uint)(*(ptr + z + 2)) << 8;
 1493051                    sum += (uint)(*(ptr + z + 1)) << 16;
 1493052                    sum += (uint)(*(ptr + z)) << 24;
 1493053                    break;
 54                case 2:
 1497055                    sum += (uint)(*(ptr + z + 1)) << 16;
 1497056                    sum += (uint)(*(ptr + z)) << 24;
 1497057                    break;
 58                case 1:
 1650059                    sum += (uint)(*(ptr + z)) << 24;
 60                    break;
 61            }
 62
 6225063            return sum;
 64        }
 65    }
 66
 67}