-
Notifications
You must be signed in to change notification settings - Fork 2
/
BitStream.cs
198 lines (168 loc) · 3.46 KB
/
BitStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.IO;
namespace Slip39;
class BitStream
{
private byte[] _buffer;
private int _writePos;
private int _readPos;
private int _lengthInBits;
public BitStream(byte[] buffer)
{
var newBuffer = new byte[buffer.Length];
Buffer.BlockCopy(buffer, 0, newBuffer, 0, buffer.Length);
_buffer = newBuffer;
_readPos = 0;
_writePos = 0;
_lengthInBits = buffer.Length * 8;
}
public void WriteBit(bool bit)
{
EnsureCapacity();
if (bit)
{
_buffer[_writePos / 8] |= (byte)(1 << (8 - (_writePos % 8) - 1));
}
_writePos++;
_lengthInBits++;
}
public void WriteBits(ulong data, byte count)
{
data <<= (64 - count);
while (count >= 8)
{
var b = (byte)(data >> (64 - 8));
WriteByte(b);
data <<= 8;
count -= 8;
}
while (count > 0)
{
var bit = data >> (64 - 1);
WriteBit(bit == 1);
data <<= 1;
count--;
}
}
public void WriteByte(byte b)
{
EnsureCapacity();
var remainCount = (_writePos % 8);
var i = _writePos / 8;
_buffer[i] |= (byte)(b >> remainCount);
var written = (8 - remainCount);
_writePos += written;
_lengthInBits += written;
if (remainCount > 0)
{
EnsureCapacity();
_buffer[i + 1] = (byte)(b << (8 - remainCount));
_writePos += remainCount;
_lengthInBits += remainCount;
}
}
public bool TryReadBit(out bool bit)
{
bit = false;
if (_readPos == _lengthInBits)
{
return false;
}
var mask = 1 << (8 - (_readPos % 8) - 1);
bit = (_buffer[_readPos / 8] & mask) == mask;
_readPos++;
return true;
}
public bool TryReadBits(int count, out ulong bits)
{
var val = 0UL;
while (count >= 8)
{
val <<= 8;
if (!TryReadByte(out var readedByte))
{
bits = 0U;
return false;
}
val |= (ulong)readedByte;
count -= 8;
}
while (count > 0)
{
val <<= 1;
if (TryReadBit(out var bit))
{
val |= bit ? 1UL : 0UL;
count--;
}
else
{
bits = 0U;
return false;
}
}
bits = val;
return true;
}
public bool TryReadByte(out byte b)
{
b = 0;
if (_readPos == _lengthInBits)
{
return false;
}
var i = _readPos / 8;
var remainCount = _readPos % 8;
b = (byte)(_buffer[i] << remainCount);
if (remainCount > 0)
{
if (i + 1 == _buffer.Length)
{
b = 0;
return false;
}
b |= (byte)(_buffer[i + 1] >> (8 - remainCount));
}
_readPos += 8;
return true;
}
public byte[] ToByteArray()
{
var arraySize = (_writePos + 7) / 8;
var byteArray = new byte[arraySize];
Array.Copy(_buffer, byteArray, arraySize);
return byteArray;
}
public int Available => _lengthInBits - _readPos;
private void EnsureCapacity()
{
if (_writePos / 8 == _buffer.Length)
{
Array.Resize(ref _buffer, _buffer.Length + (4 * 1024));
}
}
}
class BitStreamReader(BitStream stream)
{
public BitStreamReader(byte[] buffer)
: this(new BitStream(buffer))
{ }
public ulong Read(int count) =>
stream.TryReadBits(count, out var value)
? value
: throw new EndOfStreamException("There is not more bits to read.");
public byte ReadUint8(int count) => (byte)Read(count);
public ushort ReadUint16(int count) => (ushort)Read(count);
public bool CanRead(int count) => stream.Available >= count;
public bool EndOdStream => !CanRead(1);
}
class BitStreamWriter(BitStream stream)
{
public BitStreamWriter()
: this(new BitStream(new byte[100]))
{ }
public void Write(ulong data, int count) =>
stream.WriteBits(data, (byte) count);
public byte[] ToByteArray() =>
stream.ToByteArray();
}