This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitreader.go
149 lines (118 loc) · 2.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
package streaming
import (
"errors"
"io"
)
type BitReader struct {
reader io.Reader
bitOffset int
tailByte byte
err error
}
func NewBitReader(reader io.Reader) *BitReader {
return &BitReader{reader: reader}
}
func (r *BitReader) ReadBool() bool {
return r.ReadUint(1) != 0
}
func (r *BitReader) ReadIntPacked(countPacked int) int64 {
if r.err != nil {
return 0
}
count, err := unpackSize(countPacked)
if err != nil {
r.err = err
return 0
}
return r.ReadInt(count)
}
func (r *BitReader) ReadInt(count int) int64 {
return twosComplement(r.ReadUint(count), count)
}
func (r *BitReader) ReadUintPacked(countPacked int) uint64 {
if r.err != nil {
return 0
}
count, err := unpackSize(countPacked)
if err != nil {
r.err = err
return 0
}
return r.ReadUint(count)
}
func (r *BitReader) ReadUint(count int) uint64 {
if r.err != nil {
return 0
}
buffer, bitOffset, err := r.readBytes(count)
if err != nil {
r.err = err
return 0
}
return readBits(buffer, bitOffset, count)
}
func (r *BitReader) Error() error {
return r.err
}
func (r *BitReader) readBytes(count int) ([]byte, int, error) {
if count == 0 {
return nil, 0, nil
}
var (
bitOffsetInByte = r.bitOffset % 8
bitsLeftInByte = 8 - bitOffsetInByte
bytesNeeded = 1
)
if bitsLeftInByte < count {
bitsOverrun := count - bitsLeftInByte
bytesNeeded += bitsOverrun / 8
if bitsOverrun%8 != 0 {
bytesNeeded++
}
}
buffer := make([]byte, bytesNeeded)
bufferToRead := buffer
if bitsLeftInByte < 8 {
buffer[0] = r.tailByte
bufferToRead = buffer[1:]
}
if _, err := io.ReadAtLeast(r.reader, bufferToRead, len(bufferToRead)); err != nil {
return nil, 0, err
}
r.bitOffset += count
r.tailByte = buffer[bytesNeeded-1]
return buffer, bitOffsetInByte, nil
}
func readBits(buffer []byte, bitOffset, count int) uint64 {
var result uint64
remainder := count
for byteOffset := 0; remainder > 0; byteOffset++ {
bitsRead := 8 - bitOffset
if bitsRead > remainder {
bitsRead = remainder
}
bufferByte := buffer[byteOffset]
bufferByte >>= uint(bitOffset)
bufferByte &= ^(0xff << uint(bitsRead))
result |= (uint64(bufferByte) << uint(count-remainder))
remainder -= bitsRead
bitOffset = 0
}
return result
}
func twosComplement(value uint64, bits int) int64 {
signMask := uint64(1 << uint(bits-1))
if value&signMask == 0 {
return int64(value &^ signMask)
} else {
valueMask := ^(^uint64(0) << uint(bits-1))
return -int64(valueMask & (^value + 1))
}
}
func unpackSize(sizePacked int) (int, error) {
sizes := []int{0, 1, 2, 4, 6, 8, 10, 12, 14, 16, 20, 24, 26, 28, 30, 32}
if sizePacked >= len(sizes) {
return 0, errors.New("invalid packed size")
}
return sizes[sizePacked], nil
}