-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbits.go
194 lines (163 loc) · 3.14 KB
/
bits.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
// Copyright (c) 2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package gcs
import (
"io"
)
type bitWriter struct {
bytes []byte
p *byte // Pointer to last byte
next byte // Next bit to write or skip
}
// writeOne writes a one bit to the bit stream.
func (b *bitWriter) writeOne() {
if b.next == 0 {
b.bytes = append(b.bytes, 1<<7)
b.p = &b.bytes[len(b.bytes)-1]
b.next = 1 << 6
return
}
*b.p |= b.next
b.next >>= 1
}
// writeZero writes a zero bit to the bit stream.
func (b *bitWriter) writeZero() {
if b.next == 0 {
b.bytes = append(b.bytes, 0)
b.p = &b.bytes[len(b.bytes)-1]
b.next = 1 << 6
return
}
b.next >>= 1
}
// writeNBits writes n number of LSB bits of data to the bit stream in big
// endian format. Panics if n > 64.
func (b *bitWriter) writeNBits(data uint64, n uint) {
if n > 64 {
panic("gcs: cannot write more than 64 bits of a uint64")
}
data <<= 64 - n
// If byte is partially written, fill the rest
for n > 0 {
if b.next == 0 {
break
}
if data&(1<<63) != 0 {
b.writeOne()
} else {
b.writeZero()
}
n--
data <<= 1
}
if n == 0 {
return
}
// Write 8 bits at a time.
for n >= 8 {
b.bytes = append(b.bytes, byte(data>>56))
n -= 8
data <<= 8
}
// Write the remaining bits.
for n > 0 {
if data&(1<<63) != 0 {
b.writeOne()
} else {
b.writeZero()
}
n--
data <<= 1
}
}
type bitReader struct {
bytes []byte
next byte // next bit to read in bytes[0]
}
func newBitReader(bitstream []byte) bitReader {
return bitReader{
bytes: bitstream,
next: 1 << 7,
}
}
// readUnary returns the number of unread sequential one bits before the next
// zero bit. Errors with io.EOF if no zero bits are encountered.
func (b *bitReader) readUnary() (uint64, error) {
var value uint64
for {
if len(b.bytes) == 0 {
return value, io.EOF
}
for b.next != 0 {
bit := b.bytes[0] & b.next
b.next >>= 1
if bit == 0 {
return value, nil
}
value++
}
b.bytes = b.bytes[1:]
b.next = 1 << 7
}
}
// readNBits reads n number of LSB bits of data from the bit stream in big
// endian format. Panics if n > 64.
func (b *bitReader) readNBits(n uint) (uint64, error) {
if n > 64 {
panic("gcs: cannot read more than 64 bits as a uint64")
}
if len(b.bytes) == 0 {
return 0, io.EOF
}
var value uint64
// If byte is partially read, read the rest
if b.next != 1<<7 {
for n > 0 {
if b.next == 0 {
b.next = 1 << 7
b.bytes = b.bytes[1:]
break
}
n--
if b.bytes[0]&b.next != 0 {
value |= 1 << n
}
b.next >>= 1
}
}
if n == 0 {
return value, nil
}
// Read 8 bits at a time.
for n >= 8 {
if len(b.bytes) == 0 {
return 0, io.EOF
}
n -= 8
value |= uint64(b.bytes[0]) << n
b.bytes = b.bytes[1:]
}
if len(b.bytes) == 0 {
if n != 0 {
return 0, io.EOF
}
return value, nil
}
// Read the remaining bits.
for n > 0 {
if b.next == 0 {
b.bytes = b.bytes[1:]
if len(b.bytes) == 0 {
return 0, io.EOF
}
b.next = 1 << 7
}
n--
if b.bytes[0]&b.next != 0 {
value |= 1 << n
}
b.next >>= 1
}
return value, nil
}