| | | 1 | | using DeltaSharp.Util; |
| | | 2 | | using System.Buffers.Binary; |
| | | 3 | | using System.Buffers.Text; |
| | | 4 | | using System.Text; |
| | | 5 | | |
| | | 6 | | namespace DeltaSharp.Format; |
| | | 7 | | internal class FossilDeltaWriter : IDeltaWriter |
| | | 8 | | { |
| | | 9 | | |
| | 10 | 10 | | private readonly static uint[] _zDigits = { |
| | 10 | 11 | | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', |
| | 10 | 12 | | 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', |
| | 10 | 13 | | 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', 'a', 'b', 'c', 'd', 'e', |
| | 10 | 14 | | 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', |
| | 10 | 15 | | 't', 'u', 'v', 'w', 'x', 'y', 'z', '~' |
| | 10 | 16 | | }; |
| | | 17 | | |
| | | 18 | | private MemoryStream _ms; |
| | 22870 | 19 | | private byte[] _buffer => _ms.GetBuffer(); |
| | 22870 | 20 | | private int _position => (int)_ms.Position; |
| | | 21 | | |
| | 20190 | 22 | | public FossilDeltaWriter() |
| | | 23 | | { |
| | 20190 | 24 | | _ms = new MemoryStream(); |
| | 20190 | 25 | | } |
| | | 26 | | |
| | 2670 | 27 | | public FossilDeltaWriter(int sizeHint) |
| | | 28 | | { |
| | 2670 | 29 | | _ms = DeltaWriterBuffer.Create(sizeHint); |
| | 2670 | 30 | | } |
| | | 31 | | |
| | | 32 | | private unsafe void WriteInt(ulong v) |
| | | 33 | | { |
| | | 34 | | int i, j; |
| | | 35 | | |
| | 217030 | 36 | | if (v == 0) |
| | | 37 | | { |
| | 4150 | 38 | | _ms.WriteByte((byte)'0'); |
| | 4150 | 39 | | return; |
| | | 40 | | } |
| | | 41 | | |
| | 212880 | 42 | | fixed (uint* zBuf = stackalloc uint[20]) |
| | 212880 | 43 | | fixed (uint* zDigits = _zDigits) |
| | | 44 | | { |
| | 1779840 | 45 | | for (i = 0; v > 0; i++, v >>= 6) |
| | | 46 | | { |
| | 451360 | 47 | | zBuf[i] = zDigits[v & 0x3f]; |
| | | 48 | | } |
| | 1328480 | 49 | | for (j = i - 1; j >= 0; j--) |
| | | 50 | | { |
| | 451360 | 51 | | var s = (byte)zBuf[j]; |
| | 451360 | 52 | | _ms.WriteByte(s); |
| | | 53 | | } |
| | | 54 | | } |
| | 212880 | 55 | | } |
| | | 56 | | |
| | | 57 | | |
| | | 58 | | public void WriteLength(ulong length) |
| | | 59 | | { |
| | | 60 | | //write length |
| | 22870 | 61 | | WriteInt(length); |
| | | 62 | | |
| | | 63 | | //write line ending |
| | 22870 | 64 | | _ms.Write("\n"u8); |
| | 22870 | 65 | | } |
| | | 66 | | |
| | | 67 | | public void WriteCopy(ulong position, ulong length) |
| | | 68 | | { |
| | | 69 | | //write length |
| | 65170 | 70 | | WriteInt(length); |
| | | 71 | | |
| | | 72 | | //write command |
| | 65170 | 73 | | _ms.WriteByte((byte)'@'); |
| | | 74 | | |
| | | 75 | | //write copy position |
| | 65170 | 76 | | WriteInt(position); |
| | | 77 | | |
| | | 78 | | //write copy end |
| | 65170 | 79 | | _ms.WriteByte((byte)','); |
| | 65170 | 80 | | } |
| | | 81 | | |
| | | 82 | | public void WriteInsert(ReadOnlySpan<byte> s) |
| | | 83 | | { |
| | | 84 | | //write length |
| | 61050 | 85 | | WriteInt((ulong)s.Length); |
| | | 86 | | |
| | | 87 | | //write command |
| | 61050 | 88 | | _ms.WriteByte((byte)':'); |
| | | 89 | | |
| | | 90 | | //write data |
| | 61050 | 91 | | DeltaWriterBuffer.WriteLiteral(_ms, s, trailerBytes: 7); // up to six base-64 checksum digits and a semicolon |
| | 61050 | 92 | | } |
| | | 93 | | |
| | | 94 | | public void WriteChecksum(uint checksum) |
| | | 95 | | { |
| | | 96 | | //write length |
| | 2770 | 97 | | WriteInt((ulong)checksum); |
| | | 98 | | |
| | | 99 | | //write command |
| | 2770 | 100 | | _ms.WriteByte((byte)';'); |
| | | 101 | | |
| | 2770 | 102 | | } |
| | | 103 | | |
| | | 104 | | public ReadOnlyMemory<byte> GetOutput() |
| | | 105 | | { |
| | 22870 | 106 | | return _buffer.AsMemory().Slice(0,_position); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | public void Dispose() |
| | | 110 | | { |
| | 22850 | 111 | | _ms.Dispose(); |
| | 22850 | 112 | | } |
| | | 113 | | } |