-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitreader.go
547 lines (512 loc) · 13.7 KB
/
bitreader.go
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// BitReader is a simple bit reader with big/little-endian support for golang.
package bitreader
import (
"bytes"
"errors"
"io"
"math"
)
// Reader is the main structure of our Reader.
// Whenever index == 0, we need to read a new byte from stream into currentByte
//
// stream io.Reader The underlying stream we're reading bytes from
// index uint8 The current index into the byte [0-7]
// currentByte byte The byte we're currently reading from
// le bool Whether to read in little-endian order or not
type Reader struct {
stream io.Reader
index uint8
currentByte byte
littleEndian bool
}
// NewReader is the main constructor that creates the Reader object
// with stream reader data and little-endian state.
func NewReader(stream io.Reader, littleEndian bool) *Reader {
return &Reader{
stream: stream,
index: 0,
currentByte: 0,
littleEndian: littleEndian,
}
}
// NewReaderFromBytes is the main constructor that creates the Reader object
// with stream byte data and little-endian state.
func NewReaderFromBytes(stream []byte, littleEndian bool) *Reader {
return &Reader{
stream: bytes.NewReader(stream),
index: 0,
currentByte: 0,
littleEndian: littleEndian,
}
}
// Fork is a function that copies the original reader into a new reader
// with all of its current values.
func (reader *Reader) Fork() (*Reader, error) {
originalIndex := reader.index
originalCurrentByte := reader.currentByte
byteStream, err := io.ReadAll(reader.stream)
if err != nil {
return nil, err // Will only happen when there's no memory, lol
}
reader.stream = bytes.NewReader(byteStream)
return &Reader{
stream: bytes.NewReader(byteStream),
index: uint8(originalIndex),
currentByte: originalCurrentByte,
littleEndian: reader.littleEndian,
}, nil
}
// TryReadBool is a wrapper function that gets the state of 1-bit.
//
// Returns true if 1, false if 0. Panics on overflow.
func (reader *Reader) TryReadBool() bool {
flag, err := reader.ReadBool()
if err != nil {
panic(err)
}
return flag
}
// TryReadInt1 is a wrapper function that returns the value of 1-bit.
//
// Returns type uint8. Panics on overflow.
func (reader *Reader) TryReadInt1() uint8 {
value, err := reader.ReadBits(1)
if err != nil {
panic(err)
}
return uint8(value)
}
// TryReadUInt8 is a wrapper function that returns the value of 8-bits.
//
// Returns uint8. Panics on overflow.
func (reader *Reader) TryReadUInt8() uint8 {
value, err := reader.ReadBits(8)
if err != nil {
panic(err)
}
return uint8(value)
}
// TryReadSInt8 is a wrapper function that returns the value of 8-bits.
//
// Returns int8. Panics on overflow.
func (reader *Reader) TryReadSInt8() int8 {
value, err := reader.ReadBits(8)
if err != nil {
panic(err)
}
return int8(value)
}
// TryReadUInt16 is a wrapper function that returns the value of 16-bits.
//
// Returns uint16. Panics on overflow.
func (reader *Reader) TryReadUInt16() uint16 {
value, err := reader.ReadBits(16)
if err != nil {
panic(err)
}
return uint16(value)
}
// TryReadSInt16 is a wrapper function that returns the value of 16-bits.
//
// Returns uint16. Panics on overflow.
func (reader *Reader) TryReadSInt16() int16 {
value, err := reader.ReadBits(16)
if err != nil {
panic(err)
}
return int16(value)
}
// TryReadUInt32 is a wrapper function that returns the value of 32-bits.
//
// Returns uint32. Panics on overflow.
func (reader *Reader) TryReadUInt32() uint32 {
value, err := reader.ReadBits(32)
if err != nil {
panic(err)
}
return uint32(value)
}
// TryReadSInt32 is a wrapper function that returns the value of 32-bits.
//
// Returns int32. Panics on overflow.
func (reader *Reader) TryReadSInt32() int32 {
value, err := reader.ReadBits(32)
if err != nil {
panic(err)
}
return int32(value)
}
// TryReadUInt64 is a wrapper function that returns the value of 64-bits.
//
// Returns uint64. Panics on overflow.
func (reader *Reader) TryReadUInt64() uint64 {
value, err := reader.ReadBits(64)
if err != nil {
panic(err)
}
return value
}
// TryReadSInt64 is a wrapper function that returns the value of 64-bits.
//
// Returns int64. Panics on overflow.
func (reader *Reader) TryReadSInt64() int64 {
value, err := reader.ReadBits(64)
if err != nil {
panic(err)
}
return int64(value)
}
// TryReadFloat32 is a wrapper function that returns the value of 32-bits.
//
// Returns float32. Panics on overflow.
func (reader *Reader) TryReadFloat32() float32 {
value, err := reader.ReadBits(32)
if err != nil {
panic(err)
}
return math.Float32frombits(uint32(value))
}
// TryReadFloat64 is a wrapper function that returns the value of 64-bits.
//
// Returns float64. Panics on overflow.
func (reader *Reader) TryReadFloat64() float64 {
value, err := reader.ReadBits(64)
if err != nil {
panic(err)
}
return math.Float64frombits(value)
}
// TryReadBits is a wrapper function that returns the value of bits specified in the parameter.
//
// Returns uint64. Panics on overflow.
func (reader *Reader) TryReadBits(bits uint64) uint64 {
value, err := reader.ReadBits(bits)
if err != nil {
panic(err)
}
return value
}
// TryReadBytes is a wrapper function that returns the value of bits specified in the parameter.
//
// Returns uint64. Panics on overflow.
func (reader *Reader) TryReadBytes(bytes uint64) uint64 {
value, err := reader.ReadBytes(bytes)
if err != nil {
panic(err)
}
return value
}
// TryReadString is a wrapper function that returns the string
// that is read until it is null-terminated.
//
// Returns string. Panics on overflow.
func (reader *Reader) TryReadString() string {
text, err := reader.ReadString()
if err != nil {
panic(err)
}
return text
}
// TryReadStringLength is a wrapper function that returns the string
// that is read until the given length is reached or it is null-terminated.
//
// Returns string. Panics on overflow.
func (reader *Reader) TryReadStringLength(length uint64) string {
text, err := reader.ReadStringLength(length)
if err != nil {
panic(err)
}
return text
}
// TryReadBytesToSlice is a wrapper function that reads the specified amount of bits
// from the parameter and puts each bit into a slice and returns this slice.
//
// Returns []byte. Panics on overflow.
func (reader *Reader) TryReadBitsToSlice(bits uint64) []byte {
bytes := (bits / 8)
if bits%8 != 0 {
bytes++
}
out := make([]byte, bytes)
var i uint64
for i = 0; i < bytes; i++ {
if i == bytes-1 { // Not enough to fill a whole byte
if bits%8 != 0 {
val, err := reader.ReadBits(bits % 8)
if err != nil {
panic(err)
}
out[i] = byte(val)
} else {
val, err := reader.ReadBytes(1)
if err != nil {
panic(err)
}
out[i] = byte(val)
}
break
} else {
val, err := reader.ReadBytes(1)
if err != nil {
panic(err)
}
out[i] = byte(val)
}
}
return out
}
// TryReadBytesToSlice is a wrapper function that reads the specified amount of bytes
// from the parameter and puts each byte into a slice and returns this slice.
//
// Returns []byte. Panics on overflow.
func (reader *Reader) TryReadBytesToSlice(bytes uint64) []byte {
var out []byte
var i uint64
for i = 0; i < bytes; i++ {
val, err := reader.ReadBytes(1)
if err != nil {
panic(err)
}
out = append(out, byte(val))
}
return out
}
// TryReadBytesToSlice is a wrapper function that reads the remaining bits
// left in the stream and returns the count of bits.
//
// Returns uint64. Panics on overflow.
func (reader *Reader) TryReadRemainingBits() uint64 {
bits, err := reader.ReadRemainingBits()
if err != nil {
panic(err)
}
return bits
}
// ReadBool is a function that reads one bit and returns the state, error
// based on the output. Returns the read value in a bool format.
//
// Returns an error if there are no remaining bits.
func (reader *Reader) ReadBool() (bool, error) {
val, err := reader.readBit()
if err != nil {
return false, err
}
return val == 1, nil
}
// ReadBits is a function that reads the specified amount of bits
// from the parameter and returns the value, error
// based on the output. It can read up to 64 bits. Returns the read
// value in type uint64.
//
// Returns an error if there are no remaining bits.
func (reader *Reader) ReadBits(bits uint64) (uint64, error) {
if bits < 1 || bits > 64 {
return 0, errors.New("ReadBits(bits) ERROR: Bits number should be between 1 and 64")
}
var val uint64
var i uint64
for i = 0; i < bits; i++ {
bit, err := reader.readBit()
if err != nil {
return 0, err
}
if reader.littleEndian {
val |= uint64(bit) << i
} else {
val |= uint64(bit) << (bits - 1 - i)
}
}
return val, nil
}
// ReadBytes is a function that reads the specified amount of bytes
// from the parameter and returns the value, error
// based on the output. It can read up to 8 bytes. Returns the read
// value in type uint64.
//
// Returns an error if there are no remaining bits.
func (reader *Reader) ReadBytes(bytes uint64) (uint64, error) {
if bytes < 1 || bytes > 8 {
return 0, errors.New("ReadBytes(bytes) ERROR: Bytes number should be between 1 and 8")
}
value, err := reader.ReadBits(bytes * 8)
if err != nil {
return 0, err
}
return value, nil
}
// ReadString is a function that reads every byte
// until it is null-terminated (the byte is 0). Returns the
// string that is read until the null-termination.
//
// Returns an error if there are no remaining bits.
func (reader *Reader) ReadString() (string, error) {
var out []byte
for {
value, err := reader.ReadBytes(1)
if err != nil {
return string(out), err
}
if value == 0 {
break
}
out = append(out, byte(value))
}
return string(out), nil
}
// ReadStringLength is a function that reads every byte
// until the given length, or it is null-terminated (the byte is 0).
// Returns the string that is read until the lenth or null-termination.
// It will skip the remaining bytes if it is null-terminated.
//
// Returns an error if there are no remaining bits.
func (reader *Reader) ReadStringLength(length uint64) (string, error) {
var out []byte
var i uint64
for i = 0; i < length; i++ {
value, err := reader.ReadBytes(1)
if err != nil {
return string(out), err
}
if value == 0 {
reader.SkipBytes(length - 1 - i)
break
}
out = append(out, byte(value))
}
return string(out), nil
}
// ReadBitsToSlice is a function that reads the specified amount of bits
// from the parameter and puts each bit into a slice and returns this slice.
//
// Returns an error if there are no remaining bits.
func (reader *Reader) ReadBitsToSlice(bits uint64) ([]byte, error) {
bytes := (bits / 8)
if bits%8 != 0 {
bytes++
}
out := make([]byte, bytes)
var i uint64
for i = 0; i < bytes; i++ {
if i == bytes-1 { // Not enough to fill a whole byte
if bits%8 != 0 {
val, err := reader.ReadBits(bits % 8)
if err != nil {
return out, err
}
out[i] = byte(val)
} else {
val, err := reader.ReadBytes(1)
if err != nil {
return out, err
}
out[i] = byte(val)
}
break
} else {
val, err := reader.ReadBytes(1)
if err != nil {
return out, err
}
out[i] = byte(val)
}
}
return out, nil
}
// ReadBytesToSlice is a function that reads the specified amount of bytes
// from the parameter and puts each byte into a slice and returns this slice.
//
// Returns an error if there are no remaining bytes.
func (reader *Reader) ReadBytesToSlice(bytes uint64) ([]byte, error) {
var out []byte
var i uint64
for i = 0; i < bytes; i++ {
val, err := reader.ReadBytes(1)
if err != nil {
return out, err
}
out = append(out, byte(val))
}
return out, nil
}
// SkipBits is a function that increases Reader index
// based on given input bits number.
//
// Returns an error if there are no remaining bits.
func (reader *Reader) SkipBits(bits uint64) error {
// Read as many raw bytes as we can
bytes := bits / 8
if bytes > 0 {
buf := make([]byte, bytes)
_, err := reader.stream.Read(buf)
if err != nil {
return err
}
// The final read byte should be the new current byte
reader.currentByte = buf[bytes-1]
}
// Read the extra bits
for i := bytes * 8; i < bits; i++ {
_, err := reader.readBit()
if err != nil {
return err
}
}
return nil
}
// SkipBytes is a function that increases Reader index
// based on given input bytes number.
//
// Returns an error if there are no remaining bits.
func (reader *Reader) SkipBytes(bytes uint64) error {
err := reader.SkipBits(bytes * 8)
if err != nil {
return err
}
return nil
}
// ReadRemainingBits is a function that reads the total amount of remaining bits in the stream.
// It first forks the original reader to check this count, so that it does not interfere with the original stream.
//
// Returns an error if there are no remaining bits.
func (reader *Reader) ReadRemainingBits() (uint64, error) {
newReader, err := reader.Fork()
if err != nil {
return 0, err
}
var bits uint64 = 0
for {
err := newReader.SkipBits(1)
if err != nil {
break // EOF
}
bits++
}
return bits, nil
}
// readBit is a private function that reads a single bit from the stream.
// This is the main function that makes us read stream data.
func (reader *Reader) readBit() (uint8, error) {
if reader.index == 0 {
// Read a byte from stream into currentByte
buffer := make([]byte, 1)
// We are not checking for the n return value from stream.Read, because we are only reading 1 byte at a time.
// Meaning if an EOF happens with a 1 byte read, we dont have any extra byte reading anyways.
_, err := reader.stream.Read(buffer)
if err != nil {
return 0, err
}
reader.currentByte = buffer[0]
}
var val bool
if reader.littleEndian {
val = (reader.currentByte & (1 << reader.index)) != 0
} else {
val = (reader.currentByte & (1 << (7 - reader.index))) != 0
}
reader.index = (reader.index + 1) % 8
if val {
return 1, nil
} else {
return 0, nil
}
}