-
Notifications
You must be signed in to change notification settings - Fork 31
/
bitreader.go
80 lines (67 loc) · 1.32 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
package d2s
import (
"io"
)
// bitReader wraps an io.Reader and provides the ability to read bytes bit-by-bit from it.
type bitReader struct {
r io.ByteReader
n uint64
bits uint
bitsRead uint
}
func (br *bitReader) ReadByte() (byte, error) {
// Add on the total amount of bits read.
br.bitsRead += 8
return br.r.ReadByte()
}
func (br *bitReader) Reset() {
br.bitsRead = 0
}
// Align will try and align the bit reader to the nearest byte.
func (br *bitReader) Align() {
remainder := br.bitsRead % 8
if remainder > 0 {
bitsToAlign := uint(8 - remainder)
br.ReadBits(bitsToAlign)
}
}
func (br *bitReader) ReadBits(bits uint) (uint64, error) {
var n uint64
for bits > br.bits {
b, err := br.r.ReadByte()
if err == io.EOF {
return n, io.ErrUnexpectedEOF
}
if err != nil {
return n, err
}
b = reverseByte(b)
br.n <<= 8
br.n |= uint64(b)
br.bits += 8
}
n = (br.n >> (br.bits - bits)) & ((1 << bits) - 1)
n = reverseBits(n, bits)
br.bits -= bits
// Add on the total amount of bits read.
br.bitsRead += bits
return n, nil
}
func reverseByte(b byte) byte {
var d byte
for i := 0; i < 8; i++ {
d <<= 1
d |= b & 1
b >>= 1
}
return d
}
func reverseBits(b uint64, n uint) uint64 {
var d uint64
for i := 0; i < int(n); i++ {
d <<= 1
d |= b & 1
b >>= 1
}
return d
}