| | 1 | | using System.Buffers.Binary; |
| | 2 | | using System.Runtime.CompilerServices; |
| | 3 | |
|
| | 4 | | namespace NLightning.Common.Utils; |
| | 5 | |
|
| | 6 | | using Domain.Serialization; |
| | 7 | |
|
| | 8 | | public class BitWriter : IBitWriter |
| | 9 | | { |
| | 10 | | private int _bitOffset; |
| | 11 | | private byte[] _buffer; |
| | 12 | |
|
| 6428 | 13 | | public int TotalBits { get; private set; } |
| | 14 | |
|
| 228 | 15 | | public BitWriter(int totalBits) |
| | 16 | | { |
| 228 | 17 | | if (totalBits < 0) |
| 0 | 18 | | throw new ArgumentOutOfRangeException(nameof(totalBits), "Must be >= 0."); |
| | 19 | |
|
| 228 | 20 | | TotalBits = totalBits; |
| 228 | 21 | | var totalBytes = (totalBits + 7) / 8; |
| 228 | 22 | | _buffer = new byte[totalBytes]; |
| 228 | 23 | | } |
| | 24 | |
|
| | 25 | | public void GrowByBits(int additionalBits) |
| | 26 | | { |
| 4 | 27 | | var requiredBits = _bitOffset + additionalBits; |
| 4 | 28 | | if (requiredBits <= TotalBits) |
| | 29 | | { |
| 0 | 30 | | return; |
| | 31 | | } |
| | 32 | |
|
| 4 | 33 | | var newTotalBits = Math.Max(TotalBits * 2, requiredBits); |
| 4 | 34 | | var newByteCount = (newTotalBits + 7) / 8; |
| | 35 | |
|
| 4 | 36 | | Array.Resize(ref _buffer, newByteCount); |
| 4 | 37 | | TotalBits = requiredBits; |
| 4 | 38 | | } |
| | 39 | |
|
| | 40 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 41 | | public void WriteBits(ReadOnlySpan<byte> value, int bitLength) |
| | 42 | | { |
| 1200 | 43 | | WriteBits(value, 0, bitLength); |
| 1200 | 44 | | } |
| | 45 | |
|
| | 46 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 47 | | public void WriteBits(ReadOnlySpan<byte> value, int valueOffset, int bitLength) |
| | 48 | | { |
| 1200 | 49 | | if (_bitOffset + bitLength > TotalBits) |
| | 50 | | { |
| 0 | 51 | | throw new InvalidOperationException($"Not enough bits to write {bitLength}. Offset={_bitOffset}, capacity={T |
| | 52 | | } |
| | 53 | |
|
| 1200 | 54 | | var byteOffset = _bitOffset / 8; |
| 1200 | 55 | | var shift = _bitOffset % 8; |
| | 56 | |
|
| 1200 | 57 | | var bytesNeeded = (bitLength + 7) / 8; |
| | 58 | |
|
| 1200 | 59 | | if (shift == 0) |
| | 60 | | { |
| 7304 | 61 | | for (var i = 0; i < bytesNeeded; i++) |
| | 62 | | { |
| 3244 | 63 | | byte b = 0; |
| 3244 | 64 | | if (valueOffset + i < value.Length) |
| 3184 | 65 | | b = value[valueOffset + i]; |
| | 66 | |
|
| 3244 | 67 | | _buffer[byteOffset + i] = b; |
| | 68 | | } |
| | 69 | | } |
| | 70 | | else |
| | 71 | | { |
| 15200 | 72 | | for (var i = 0; i < bytesNeeded; i++) |
| | 73 | | { |
| 6808 | 74 | | byte current = 0; |
| 6808 | 75 | | if (valueOffset + i < value.Length) |
| 6696 | 76 | | current = value[valueOffset + i]; |
| | 77 | |
|
| 6808 | 78 | | var left = (byte)(current >> shift); |
| 6808 | 79 | | _buffer[byteOffset + i] |= left; |
| | 80 | |
|
| 6808 | 81 | | var nextIndex = byteOffset + i + 1; |
| | 82 | |
|
| 6808 | 83 | | if (nextIndex >= _buffer.Length) continue; |
| | 84 | |
|
| 6808 | 85 | | var right = (byte)(current << (8 - shift)); |
| 6808 | 86 | | _buffer[nextIndex] |= right; |
| | 87 | | } |
| | 88 | | } |
| | 89 | |
|
| 1200 | 90 | | _bitOffset += bitLength; |
| 1200 | 91 | | } |
| | 92 | |
|
| | 93 | | public void WriteBit(bool bit) |
| | 94 | | { |
| 2352 | 95 | | if (_bitOffset >= TotalBits) |
| | 96 | | { |
| 4 | 97 | | throw new InvalidOperationException("No more bits to write."); |
| | 98 | | } |
| | 99 | |
|
| 2348 | 100 | | var byteIndex = _bitOffset / 8; |
| 2348 | 101 | | var bitIndex = _bitOffset % 8; |
| 2348 | 102 | | var shift = 7 - bitIndex; |
| | 103 | |
|
| 2348 | 104 | | if (bit) |
| | 105 | | { |
| 164 | 106 | | _buffer[byteIndex] |= (byte)(1 << shift); |
| | 107 | | } |
| | 108 | | else |
| | 109 | | { |
| 2184 | 110 | | _buffer[byteIndex] &= (byte)~(1 << shift); |
| | 111 | | } |
| | 112 | |
|
| 2348 | 113 | | _bitOffset++; |
| 2348 | 114 | | } |
| | 115 | |
|
| | 116 | | public void WriteByteAsBits(byte value, int bits) |
| | 117 | | { |
| | 118 | | const byte BYTE_BIT_SIZE = sizeof(byte) * 8; |
| 340 | 119 | | if (bits is < 1 or > BYTE_BIT_SIZE) |
| 0 | 120 | | throw new ArgumentOutOfRangeException(nameof(bits), $"must be between 1 and {BYTE_BIT_SIZE}."); |
| | 121 | |
|
| 340 | 122 | | var masked = value & (byte)((1 << bits) - 1); |
| 340 | 123 | | var shifted = (byte)(masked << (BYTE_BIT_SIZE - bits)); |
| | 124 | |
|
| 340 | 125 | | Span<byte> bytes = stackalloc byte[sizeof(byte)]; |
| 340 | 126 | | bytes[0] = shifted; |
| | 127 | |
|
| 340 | 128 | | WriteBits(bytes, bits); |
| 340 | 129 | | } |
| | 130 | |
|
| | 131 | | public void WriteInt16AsBits(short value, int bits, bool bigEndian = true) |
| | 132 | | { |
| | 133 | | const byte SHORT_BIT_SIZE = sizeof(short) * 8; |
| 324 | 134 | | if (bits is < 1 or > SHORT_BIT_SIZE) |
| 0 | 135 | | throw new ArgumentOutOfRangeException(nameof(bits), $"must be between 1 and {SHORT_BIT_SIZE}."); |
| | 136 | |
|
| 324 | 137 | | var masked = value & (short)((1 >> bits) - 1); |
| 324 | 138 | | var shifted = (short)(masked << (SHORT_BIT_SIZE - bits)); |
| | 139 | |
|
| 324 | 140 | | Span<byte> bytes = stackalloc byte[sizeof(short)]; |
| 324 | 141 | | BinaryPrimitives.WriteInt16LittleEndian(bytes, shifted); |
| | 142 | |
|
| 324 | 143 | | if ((bigEndian && BitConverter.IsLittleEndian) || (!bigEndian && !BitConverter.IsLittleEndian)) |
| | 144 | | { |
| 324 | 145 | | bytes.Reverse(); |
| | 146 | | } |
| | 147 | |
|
| 324 | 148 | | WriteBits(bytes, bits); |
| 324 | 149 | | } |
| | 150 | |
|
| | 151 | | public void WriteUInt16AsBits(ushort value, int bits, bool bigEndian = true) |
| | 152 | | { |
| | 153 | | const byte USHORT_BIT_SIZE = sizeof(ushort) * 8; |
| 24 | 154 | | if (bits is < 1 or > USHORT_BIT_SIZE) |
| 0 | 155 | | throw new ArgumentOutOfRangeException(nameof(bits), $"must be between 1 and {USHORT_BIT_SIZE}."); |
| | 156 | |
|
| 24 | 157 | | var masked = value & (ushort)((1 << bits) - 1); |
| 24 | 158 | | var shifted = (ushort)(masked << (USHORT_BIT_SIZE - bits)); |
| | 159 | |
|
| 24 | 160 | | Span<byte> bytes = stackalloc byte[sizeof(ushort)]; |
| 24 | 161 | | BinaryPrimitives.WriteUInt16LittleEndian(bytes, shifted); |
| | 162 | |
|
| 24 | 163 | | if ((bigEndian && BitConverter.IsLittleEndian) || (!bigEndian && !BitConverter.IsLittleEndian)) |
| | 164 | | { |
| 24 | 165 | | bytes.Reverse(); |
| | 166 | | } |
| | 167 | |
|
| 24 | 168 | | WriteBits(bytes, bits); |
| 24 | 169 | | } |
| | 170 | |
|
| | 171 | | public void WriteInt32AsBits(int value, int bits, bool bigEndian = true) |
| | 172 | | { |
| | 173 | | const byte INT_BIT_SIZE = sizeof(int) * 8; |
| 92 | 174 | | if (bits is < 1 or > INT_BIT_SIZE) |
| 0 | 175 | | throw new ArgumentOutOfRangeException(nameof(bits), $"must be between 1 and {INT_BIT_SIZE}."); |
| | 176 | |
|
| 92 | 177 | | var masked = value & (int)((1L << bits) - 1); |
| 92 | 178 | | var shifted = masked << (INT_BIT_SIZE - bits); |
| | 179 | |
|
| 92 | 180 | | Span<byte> bytes = stackalloc byte[sizeof(int)]; |
| 92 | 181 | | BinaryPrimitives.WriteInt32LittleEndian(bytes, shifted); |
| | 182 | |
|
| 92 | 183 | | if ((bigEndian && BitConverter.IsLittleEndian) || (!bigEndian && !BitConverter.IsLittleEndian)) |
| | 184 | | { |
| 92 | 185 | | bytes.Reverse(); |
| | 186 | | } |
| | 187 | |
|
| 92 | 188 | | WriteBits(bytes, bits); |
| 92 | 189 | | } |
| | 190 | |
|
| | 191 | | public void WriteInt64AsBits(long value, int bits, bool bigEndian = true) |
| | 192 | | { |
| | 193 | | const byte LONG_BIT_SIZE = sizeof(long) * 8; |
| 72 | 194 | | if (bits is < 1 or > LONG_BIT_SIZE) |
| 0 | 195 | | throw new ArgumentOutOfRangeException(nameof(bits), $"must be between 1 and {LONG_BIT_SIZE}."); |
| | 196 | |
|
| 72 | 197 | | var masked = value & (long)((1UL << bits) - 1); |
| 72 | 198 | | var shifted = masked << (LONG_BIT_SIZE - bits); |
| | 199 | |
|
| 72 | 200 | | Span<byte> bytes = stackalloc byte[sizeof(long)]; |
| 72 | 201 | | BinaryPrimitives.WriteInt64LittleEndian(bytes, shifted); |
| | 202 | |
|
| 72 | 203 | | if ((bigEndian && BitConverter.IsLittleEndian) || (!bigEndian && !BitConverter.IsLittleEndian)) |
| | 204 | | { |
| 72 | 205 | | bytes.Reverse(); |
| | 206 | | } |
| | 207 | |
|
| 72 | 208 | | WriteBits(bytes, bits); |
| 72 | 209 | | } |
| | 210 | |
|
| | 211 | | public bool HasMoreBits(int requiredBits) |
| | 212 | | { |
| 2272 | 213 | | return _bitOffset + requiredBits <= TotalBits; |
| | 214 | | } |
| | 215 | |
|
| | 216 | | public void SkipBits(int v) |
| | 217 | | { |
| 0 | 218 | | _bitOffset += v; |
| 0 | 219 | | } |
| | 220 | |
|
| | 221 | | public byte[] ToArray() |
| | 222 | | { |
| 284 | 223 | | var bytes = new byte[(TotalBits + 7) / 8]; |
| 37648 | 224 | | for (var i = 0; i < bytes.Length; i++) |
| | 225 | | { |
| 18540 | 226 | | bytes[i] = _buffer[i]; |
| | 227 | | } |
| | 228 | |
|
| 284 | 229 | | return bytes; |
| | 230 | | } |
| | 231 | | } |