| | | 1 | | using DeltaSharp.Util; |
| | | 2 | | using System.Buffers.Binary; |
| | | 3 | | using System.Buffers.Text; |
| | | 4 | | using System.Text; |
| | | 5 | | |
| | | 6 | | namespace 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> |
| | | 10 | | public class FossilDeltaReader : IDeltaReader |
| | | 11 | | { |
| | | 12 | | |
| | 10 | 13 | | static readonly int[] _zValue = { |
| | 10 | 14 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| | 10 | 15 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| | 10 | 16 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| | 10 | 17 | | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| | 10 | 18 | | -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| | 10 | 19 | | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36, |
| | 10 | 20 | | -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, |
| | 10 | 21 | | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1 |
| | 10 | 22 | | }; |
| | | 23 | | |
| | | 24 | | /// <summary>Initializes a Fossil delta reader.</summary> |
| | 340 | 25 | | public FossilDeltaReader() |
| | | 26 | | { |
| | | 27 | | |
| | 340 | 28 | | } |
| | | 29 | | |
| | | 30 | | private unsafe (uint value, uint consumed) ReadInt(ReadOnlySpan<byte> input) |
| | | 31 | | { |
| | 297180 | 32 | | uint consumed = 0; |
| | 297180 | 33 | | fixed (int* zValue = _zValue) |
| | | 34 | | { |
| | 297180 | 35 | | uint v = 0; |
| | | 36 | | int c; |
| | 870880 | 37 | | 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. |
| | 573720 | 42 | | if ((v & 0xFC00_0000u) != 0) |
| | 20 | 43 | | throw new DeltaReaderException("integer too large in delta"); |
| | | 44 | | |
| | 573700 | 45 | | v = (v << 6) + (uint)c; |
| | 573700 | 46 | | consumed++; |
| | 573700 | 47 | | input = input.Slice(1); |
| | | 48 | | } |
| | 297160 | 49 | | 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 | | { |
| | 221680 | 58 | | command = default; |
| | 221680 | 59 | | consumed = 0; |
| | | 60 | | |
| | 221680 | 61 | | if (input.IsEmpty) |
| | 320 | 62 | | return false; |
| | | 63 | | |
| | 221360 | 64 | | (var cnt, var read) = ReadInt(input); |
| | 221340 | 65 | | consumed += read; |
| | 221340 | 66 | | 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. |
| | 221340 | 72 | | if (input.IsEmpty) |
| | 240 | 73 | | return false; |
| | | 74 | | |
| | 221100 | 75 | | var cmd = input[0]; |
| | 221100 | 76 | | consumed++; |
| | | 77 | | |
| | 221100 | 78 | | input = input.Slice(1); |
| | | 79 | | |
| | 221100 | 80 | | if (cmd == (byte)'\n') |
| | | 81 | | { |
| | 53290 | 82 | | command = new DeltaCommand(DeltaCommandEnum.Length, 0, cnt); |
| | | 83 | | |
| | 53290 | 84 | | return true; |
| | | 85 | | } |
| | 167810 | 86 | | if (cmd == (byte)'@') |
| | | 87 | | { |
| | 75820 | 88 | | (var offset, read) = ReadInt(input); |
| | 75820 | 89 | | consumed += read; |
| | | 90 | | |
| | 75820 | 91 | | input = input.Slice((int)read); |
| | 75820 | 92 | | if (input.IsEmpty || input[0] != ',') |
| | 90 | 93 | | throw new DeltaReaderException("copy command not terminated by ','"); |
| | | 94 | | |
| | 75730 | 95 | | input = input.Slice(1); |
| | 75730 | 96 | | 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. |
| | 75730 | 100 | | command = new DeltaCommand(DeltaCommandEnum.Copy, offset, cnt); |
| | | 101 | | |
| | 75730 | 102 | | return true; |
| | | 103 | | |
| | | 104 | | } |
| | 91990 | 105 | | else if (cmd == (byte)':') |
| | | 106 | | { |
| | 67500 | 107 | | command = new DeltaCommand(DeltaCommandEnum.Insert, 0, cnt); |
| | | 108 | | |
| | 67500 | 109 | | return true; |
| | | 110 | | } |
| | 24490 | 111 | | else if (cmd == (byte)';') |
| | | 112 | | { |
| | 5140 | 113 | | command = new DeltaCommand(DeltaCommandEnum.Checksum, 0, cnt); |
| | | 114 | | |
| | 5140 | 115 | | return true; |
| | | 116 | | } |
| | | 117 | | |
| | 19350 | 118 | | return false; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | |
| | | 122 | | } |
| | | 123 | | |