128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
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
|
+
-
+
+
+
+
+
+
+
+
-
+
|
continue
}
// Clearing e.length is not necessary as it will be overwritten later
// Stage the complete byte for writing
buffer := make([]byte, 1, 2)
buffer := []byte{byte(e.remaining)}
buffer[0] = byte(e.remaining)
// Stage every full byte from the encoded value for writing
//
// The bitlength of the largest encoded byte value, 255, is 13.
// Even with 7 bits already in the buffer this leaves [7+1], [8]
// and 4 bits remaining => a single if is enough instead of a for.
//
// 128 is [1000 0000] in binary. Any value equal or greater than it
// will be atleast 8 bits in length
for enc > 128 {
if enc >= 128 {
buffer = append(buffer, byte(enc))
enc >>= 8
len -= 8
}
// Store the remaining bits
e.remaining, e.length = byte(enc), len
|
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
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
|
+
+
+
|
output[0] = byte(val)
// Advance the internal and output buffers
output = output[1:]
d.buffer >>= len
d.at -= len
// Account for the processed output byte
total++
}
// Termination condition
if len(output) == 0 || err != nil {
return total, err
}
// We need to limit the output's size else we could end up with a lot of small values
// that fit neither in the output slice nor in the internal buffer
//
// (63 is [0011 1111] in binary, xor is a substraction and right shift a division)
free := int((63 ^ d.at) >> 3)
if free > len(output) {
free = len(output)
}
// Read data and transfer to the internal buffer
count, err := d.source.Read(output[:free])
|