< Summary

Information
Class: DeltaSharp.Format.FossilDeltaReader
Assembly: DeltaSharp
File(s): /home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Format/Fossil/FossilDeltaReader.cs
Line coverage
100%
Covered lines: 54
Uncovered lines: 0
Coverable lines: 54
Total lines: 123
Line coverage: 100%
Branch coverage
92%
Covered branches: 26
Total branches: 28
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor()100%11100%
ReadInt(...)83.33%1212100%
TryRead(...)100%1616100%

File(s)

/home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Format/Fossil/FossilDeltaReader.cs

#LineLine coverage
 1using DeltaSharp.Util;
 2using System.Buffers.Binary;
 3using System.Buffers.Text;
 4using System.Text;
 5
 6namespace DeltaSharp.Format;
 7
 8/// <summary>Reads Fossil delta command headers with unsigned 32-bit base-64 integer operands.</summary>
 9/// <remarks>A copy count of zero copies no bytes. Insert payloads are raw bytes.</remarks>
 10public class FossilDeltaReader : IDeltaReader
 11{
 12
 1013    static readonly int[] _zValue = {
 1014            -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
 1015            -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
 1016            -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
 1017            0,  1,  2,  3,  4,  5,  6,  7,    8,  9, -1, -1, -1, -1, -1, -1,
 1018            -1, 10, 11, 12, 13, 14, 15, 16,   17, 18, 19, 20, 21, 22, 23, 24,
 1019            25, 26, 27, 28, 29, 30, 31, 32,   33, 34, 35, -1, -1, -1, -1, 36,
 1020            -1, 37, 38, 39, 40, 41, 42, 43,   44, 45, 46, 47, 48, 49, 50, 51,
 1021            52, 53, 54, 55, 56, 57, 58, 59,   60, 61, 62, -1, -1, -1, 63, -1
 1022        };
 23
 24    /// <summary>Initializes a Fossil delta reader.</summary>
 34025    public FossilDeltaReader()
 26    {
 27
 34028    }
 29
 30    private unsafe (uint value, uint consumed) ReadInt(ReadOnlySpan<byte> input)
 31    {
 29718032        uint consumed = 0;
 29718033        fixed (int* zValue = _zValue)
 34        {
 29718035            uint v = 0;
 36            int c;
 87088037            while (!input.IsEmpty && input[0] < 128 && (c = zValue[input[0]]) >= 0)
 38            {
 39                // Each digit shifts the accumulator left by 6 bits. If the top 6 bits
 40                // are already set, another digit would silently overflow the 32-bit
 41                // value and corrupt the parsed length/offset. Reject instead.
 57372042                if ((v & 0xFC00_0000u) != 0)
 2043                    throw new DeltaReaderException("integer too large in delta");
 44
 57370045                v = (v << 6) + (uint)c;
 57370046                consumed++;
 57370047                input = input.Slice(1);
 48            }
 29716049            return (v, consumed);
 50        }
 51
 52
 53    }
 54
 55    /// <inheritdoc cref="IDeltaReader.TryRead"/>
 56    public bool TryRead(ReadOnlySpan<byte> input, out DeltaCommand command, out uint consumed)
 57    {
 22168058        command = default;
 22168059        consumed = 0;
 60
 22168061        if (input.IsEmpty)
 32062            return false;
 63
 22136064        (var cnt, var read) = ReadInt(input);
 22134065        consumed += read;
 22134066        input = input.Slice((int)read);
 67
 68        // ReadInt may have consumed every remaining byte (e.g. a delta that is all
 69        // base64 digits and carries no command byte). Guard before dereferencing,
 70        // otherwise input[0] throws IndexOutOfRangeException instead of cleanly
 71        // reporting an invalid/incomplete delta via the TryRead contract.
 22134072        if (input.IsEmpty)
 24073            return false;
 74
 22110075        var cmd = input[0];
 22110076        consumed++;
 77
 22110078        input = input.Slice(1);
 79
 22110080        if (cmd == (byte)'\n')
 81        {
 5329082            command = new DeltaCommand(DeltaCommandEnum.Length, 0, cnt);
 83
 5329084            return true;
 85        }
 16781086        if (cmd == (byte)'@')
 87        {
 7582088            (var offset, read) = ReadInt(input);
 7582089            consumed += read;
 90
 7582091            input = input.Slice((int)read);
 7582092            if (input.IsEmpty || input[0] != ',')
 9093                throw new DeltaReaderException("copy command not terminated by ','");
 94
 7573095            input = input.Slice(1);
 7573096            consumed++;//account for ','
 97
 98            // Match Fossil's reference decoder: zero copies no bytes, not the
 99            // rest of the source. Apply still validates the offset and output.
 75730100            command = new DeltaCommand(DeltaCommandEnum.Copy, offset, cnt);
 101
 75730102            return true;
 103
 104        }
 91990105        else if (cmd == (byte)':')
 106        {
 67500107            command = new DeltaCommand(DeltaCommandEnum.Insert, 0, cnt);
 108
 67500109            return true;
 110        }
 24490111        else if (cmd == (byte)';')
 112        {
 5140113            command = new DeltaCommand(DeltaCommandEnum.Checksum, 0, cnt);
 114
 5140115            return true;
 116        }
 117
 19350118        return false;
 119    }
 120
 121
 122}
 123