| | | 1 | | using DeltaSharp.Util; |
| | | 2 | | |
| | | 3 | | namespace DeltaSharp.Format; |
| | | 4 | | |
| | | 5 | | /// <summary>Reads DeltaSharp binary command headers with variable-length integer operands.</summary> |
| | | 6 | | public class BinaryDeltaReader : IDeltaReader |
| | | 7 | | { |
| | | 8 | | |
| | | 9 | | /// <inheritdoc cref="IDeltaReader.TryRead"/> |
| | | 10 | | public bool TryRead(ReadOnlySpan<byte> input, out DeltaCommand command, out uint consumed) |
| | | 11 | | { |
| | 234170 | 12 | | command = default; |
| | 234170 | 13 | | consumed = 0; |
| | | 14 | | |
| | 234170 | 15 | | if (input.IsEmpty) |
| | 300 | 16 | | return false; |
| | | 17 | | |
| | 233870 | 18 | | var commandChar = (char)input[0]; |
| | | 19 | | |
| | | 20 | | switch (commandChar) |
| | | 21 | | { |
| | | 22 | | case 'l': |
| | | 23 | | { |
| | 53100 | 24 | | input = input.Slice(1); |
| | 53100 | 25 | | if (!VarIntFormatter.TryDecode(input, out var length, out var lengthChars)) |
| | 10 | 26 | | return false; |
| | | 27 | | |
| | 53090 | 28 | | consumed = (uint)lengthChars + 1; |
| | | 29 | | |
| | 53090 | 30 | | command = new DeltaCommand(DeltaCommandEnum.Length, 0, length); |
| | | 31 | | |
| | 53090 | 32 | | return true; |
| | | 33 | | } |
| | | 34 | | case 'c': |
| | | 35 | | { |
| | 82150 | 36 | | input = input.Slice(1); |
| | 82150 | 37 | | if (!VarIntFormatter.TryDecode(input, out var position, out var positionBytes)) |
| | 0 | 38 | | return false; |
| | | 39 | | |
| | 82150 | 40 | | input = input.Slice(positionBytes); |
| | 82150 | 41 | | if (!VarIntFormatter.TryDecode(input, out var length, out var lengthBytes)) |
| | 0 | 42 | | return false; |
| | | 43 | | |
| | 82150 | 44 | | consumed = (uint)positionBytes + (uint)lengthBytes + 1; |
| | | 45 | | |
| | 82150 | 46 | | command = new DeltaCommand(DeltaCommandEnum.Copy, position, length); |
| | | 47 | | |
| | 82150 | 48 | | return true; |
| | | 49 | | } |
| | | 50 | | case 'i': |
| | | 51 | | { |
| | 74000 | 52 | | input = input.Slice(1); |
| | 74000 | 53 | | if (!VarIntFormatter.TryDecode(input, out var length, out var lengthBytes)) |
| | 10 | 54 | | return false; |
| | | 55 | | |
| | 73990 | 56 | | consumed = (uint)lengthBytes + 1; |
| | | 57 | | |
| | 73990 | 58 | | command = new DeltaCommand(DeltaCommandEnum.Insert, 0, length); |
| | | 59 | | |
| | 73990 | 60 | | return true; |
| | | 61 | | } |
| | | 62 | | case 'v': |
| | | 63 | | { |
| | 5140 | 64 | | input = input.Slice(1); |
| | 5140 | 65 | | if (!VarIntFormatter.TryDecode(input, out var length, out var lengthBytes)) |
| | 50 | 66 | | return false; |
| | | 67 | | |
| | 5090 | 68 | | consumed = (uint)lengthBytes + 1; |
| | | 69 | | |
| | 5090 | 70 | | command = new DeltaCommand(DeltaCommandEnum.Checksum, 0, length); |
| | | 71 | | |
| | 5090 | 72 | | return true; |
| | | 73 | | } |
| | | 74 | | default: |
| | 19480 | 75 | | throw new DeltaReaderException("Invalid command"); |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary>Reports invalid syntax or an unsupported numeric value in an encoded delta command.</summary> |
| | | 82 | | public class DeltaReaderException : Exception |
| | | 83 | | { |
| | | 84 | | /// <summary>Initializes a reader exception with the default exception message.</summary> |
| | | 85 | | public DeltaReaderException() { } |
| | | 86 | | |
| | | 87 | | /// <summary>Initializes a reader exception with a description of the parsing failure.</summary> |
| | | 88 | | /// <param name="message">The description of the invalid command.</param> |
| | | 89 | | public DeltaReaderException(string message) : base(message) { } |
| | | 90 | | } |