-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreader.go
533 lines (451 loc) · 12.3 KB
/
reader.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
package bitstream
import (
"fmt"
"io"
"github.com/pkg/errors"
)
const (
DefaultBufferSize = 1024
)
// Reader is a bit stream reader.
// It does not have io.Reader interface.
type Reader struct {
src io.Reader
srcEOF bool
buf []byte
bufLen uint
currByteIndex uint // starts from 0
currBitIndex uint8 // MSB: 7, LSB: 0
consumedBytes uint
opt *ReaderOptions
}
// ReaderOptions is a set of options for creating a Reader.
type ReaderOptions struct {
BufferSize uint
}
// GetBufferSize gets configured buffer size.
func (opt *ReaderOptions) GetBufferSize() uint {
if opt == nil || opt.BufferSize == 0 {
return DefaultBufferSize
}
return opt.BufferSize
}
// NewReader creates a new Reader instance with options.
func NewReader(src io.Reader, opt *ReaderOptions) *Reader {
return &Reader{
src: src,
srcEOF: false,
buf: nil,
bufLen: 0,
currByteIndex: 0,
currBitIndex: 7,
opt: opt,
}
}
func (r *Reader) dump() {
fmt.Printf("srcEOF=%t, bufLen=%d, currByteIndex=%d, currBitIndex=%d\n", r.srcEOF, r.bufLen, r.currByteIndex, r.currBitIndex)
}
func (r *Reader) isBufEmpty() bool {
if r.buf == nil {
return true
}
if r.currByteIndex >= r.bufLen {
return true
}
return false
}
func (r *Reader) fillBuf() error {
buf := make([]byte, r.opt.GetBufferSize())
n, err := r.src.Read(buf[:])
if err != nil {
return err
}
r.buf = buf
r.bufLen = uint(n)
r.currByteIndex = 0
r.currBitIndex = 7
return nil
}
func (r *Reader) fillBufIfNeeded() error {
if !r.isBufEmpty() {
return nil
}
return r.fillBuf()
}
func (r *Reader) forwardIndecies(nBits uint8) {
if nBits <= r.currBitIndex {
r.currBitIndex -= nBits
return
}
nBits = nBits - r.currBitIndex
nBytes := uint(nBits/8) + 1
r.currByteIndex += nBytes
r.consumedBytes += nBytes
bitsToGo := (nBits % 8)
r.currBitIndex = 8 - bitsToGo
}
// ConsumedBytes returns a number of bytes that has been consumed.
func (r *Reader) ConsumedBytes() uint {
if r.currBitIndex != 7 {
return r.consumedBytes + 1
}
return r.consumedBytes
}
// ReadBit reads a single bit from the bit stream.
// The bit read from the stream will be set in the LSB of the return value.
func (r *Reader) ReadBit() (byte, error) {
err := r.fillBufIfNeeded()
if err != nil {
return 0, err
}
b := r.buf[r.currByteIndex]
mask := uint8(1 << r.currBitIndex)
result := (b & mask) >> r.currBitIndex
r.forwardIndecies(1)
return result, nil
}
// ReadBool reads a single bit from the bit stream and return it as a bool.
func (r *Reader) ReadBool() (bool, error) {
b, err := r.ReadBit()
if err != nil {
return false, err
}
return b != 0, nil
}
func (r *Reader) mustReadNBitsInCurrentByte(nBits uint8) byte {
if nBits == 0 {
return 0
}
if r.currBitIndex < (nBits - 1) {
panic(fmt.Sprintf("%+v", errors.New("insufficient bits to read")))
}
b := r.buf[r.currByteIndex]
mask := uint8((1 << (r.currBitIndex + 1)) - 1)
result := (b & mask) >> (r.currBitIndex - (nBits - 1))
r.forwardIndecies(nBits)
return result
}
// ReadNBitsAsUint8 reads `nBits` bits as a unsigned integer from the bit stream and returns it in uint8 (LSB aligned).
// `nBits` must be less than or equal to 8, otherwise returns an error.
// If `nBits` == 0, this function always returns 0.
func (r *Reader) ReadNBitsAsUint8(nBits uint8) (uint8, error) {
if nBits == 0 {
return 0, nil
}
if nBits > 8 {
return 0, errors.New("nBits too large for uint8")
}
err := r.fillBufIfNeeded()
if err != nil {
return 0, err
}
// remaining bits in current byte
rb := r.currBitIndex + 1
if nBits <= rb { // can be read from the current byte
b := r.mustReadNBitsInCurrentByte(nBits)
return b, nil
}
// 8 bits are distributed in 2 bytes
nBits1 := rb
nBits2 := nBits - rb
b1 := r.mustReadNBitsInCurrentByte(nBits1)
b2, err := r.ReadNBitsAsUint8(nBits2)
if err != nil {
return 0, err
}
return (b1 << nBits2) | b2, nil
}
// ReadUint8 reads 8 bits from the bit stream and returns it in uint8.
func (r *Reader) ReadUint8() (uint8, error) {
return r.ReadNBitsAsUint8(8)
}
// ReadNBitsAsUint16BE reads `nBits` bits as a big endian unsigned integer from the bit stream and returns it in uint16 (LSB aligned).
// `nBits` must be less than or equal to 16, otherwise returns an error.
// If `nBits` == 0, this function always returns 0.
func (r *Reader) ReadNBitsAsUint16BE(nBits uint8) (uint16, error) {
if nBits == 0 {
return 0, nil
}
if nBits <= 8 {
v, err := r.ReadNBitsAsUint8(nBits)
return uint16(v), err
}
if nBits > 16 {
return 0, errors.New("nBits too large for uint16")
}
err := r.fillBufIfNeeded()
if err != nil {
return 0, err
}
// remaining bits in current byte
rb := r.currBitIndex + 1
// 16 bits may be distributed in up to 3 bytes
nBits1 := rb // count of bits in the first byte
nBits2 := nBits - rb // count of bits in the second byte
nBits3 := uint8(0) // count of bits in the third byte
if nBits2 > 8 {
nBits3 = nBits2 - 8
nBits2 = 8
}
b1 := r.mustReadNBitsInCurrentByte(nBits1)
b2, err := r.ReadNBitsAsUint8(nBits2)
if err != nil {
return 0, err
}
b3, err := r.ReadNBitsAsUint8(nBits3) // expects this function returns 0 if nBits3 == 0
if err != nil {
return 0, err
}
return (uint16(b1) << (nBits2 + nBits3)) | (uint16(b2) << nBits3) | uint16(b3), nil
}
// ReadUint16BE reads 16 bits as a big endian unsigned integer from the bit stream and returns it in uint16.
func (r *Reader) ReadUint16BE() (uint16, error) {
return r.ReadNBitsAsUint16BE(16)
}
// ReadNBitsAsUint32BE reads `nBits` bits as a big endian unsigned integer from the bit stream and returns it in uint32 (LSB aligned).
// `nBits` must be less than or equal to 32, otherwise returns an error.
// If `nBits` == 0, this function always returns 0.
func (r *Reader) ReadNBitsAsUint32BE(nBits uint8) (uint32, error) {
if nBits == 0 {
return 0, nil
}
if nBits <= 16 {
v, err := r.ReadNBitsAsUint16BE(nBits)
return uint32(v), err
}
if nBits > 32 {
return 0, errors.New("nBits too large for uint32")
}
err := r.fillBufIfNeeded()
if err != nil {
return 0, err
}
// remaining bits in current byte
rb := r.currBitIndex + 1
// 32 bits may be distributed in up to 5 bytes
nBits1 := rb
nBits2 := uint8(8)
nBits3 := nBits - rb - 8
nBits4 := uint8(0)
nBits5 := uint8(0)
if nBits3 > 8 {
nBits4 = nBits3 - 8
if nBits4 > 8 {
nBits5 = nBits4 - 8
nBits4 = 8
}
nBits3 = 8
}
b1 := r.mustReadNBitsInCurrentByte(nBits1)
b2, err := r.ReadNBitsAsUint8(nBits2)
if err != nil {
return 0, err
}
b3, err := r.ReadNBitsAsUint8(nBits3)
if err != nil {
return 0, err
}
b4, err := r.ReadNBitsAsUint8(nBits4)
if err != nil {
return 0, err
}
b5, err := r.ReadNBitsAsUint8(nBits5)
if err != nil {
return 0, err
}
return (uint32(b1) << (nBits2 + nBits3 + nBits4 + nBits5)) | (uint32(b2) << (nBits3 + nBits4 + nBits5)) | (uint32(b3) << (nBits4 + nBits5)) | (uint32(b4) << (nBits5)) | uint32(b5), nil
}
// ReadUint32BE reads 32 bits as a big endian unsigned integer from the bit stream and returns it in uint32.
func (r *Reader) ReadUint32BE() (uint32, error) {
return r.ReadNBitsAsUint32BE(32)
}
// ReadNBitsAsInt32BE reads `nBits` bits as a big endian signed integer from the bit stream and returns it in int32 (LSB aligned).
// MSB is a sign bit.
// `nBits` must be less than or equal to 32, otherwise returns an error.
// If `nBits` == 0, this function always returns 0.
func (r *Reader) ReadNBitsAsInt32BE(nBits uint8) (int32, error) {
v, err := r.ReadNBitsAsUint32BE(nBits)
if err != nil {
return 0, err
}
//fmt.Printf("v == %#08x\n", v)
msb := uint32(1) << (nBits - 1)
//fmt.Printf("msb == %#08x\n", msb)
if (v & msb) == 0 {
return int32(v), nil
}
f := 0xffffffff & ^(msb - 1)
//fmt.Printf("f ==%#08x\n", f)
//fmt.Printf("f|v ==%#08x\n", f|v)
return int32(f | v), nil
}
// ReadNBitsAsUint64BE reads `nBits` bits as a big endian unsigned integer from the bit stream and returns it in uint64 (LSB aligned).
// `nBits` must be less than or equal to 64, otherwise returns an error.
// If `nBits` == 0, this function always returns 0.
func (r *Reader) ReadNBitsAsUint64BE(nBits uint8) (uint64, error) {
if nBits == 0 {
return 0, nil
}
if nBits <= 32 {
v, err := r.ReadNBitsAsUint32BE(nBits)
return uint64(v), err
}
if nBits > 64 {
return 0, errors.New("nBits too large for uint64")
}
err := r.fillBufIfNeeded()
if err != nil {
return 0, err
}
// remaining bits in current byte
rb := r.currBitIndex + 1
// 64bit value may be distributed in 9 bytes
nBits1 := rb
nBits2 := uint8(8)
nBits3 := uint8(8)
nBits4 := uint8(8)
nBits5 := nBits - rb - 24
nBits6 := uint8(0)
nBits7 := uint8(0)
nBits8 := uint8(0)
nBits9 := uint8(0)
if nBits5 > 8 {
nBits6 = nBits5 - 8
if nBits6 > 8 {
nBits7 = nBits6 - 8
if nBits7 > 8 {
nBits8 = nBits7 - 8
if nBits8 > 8 {
nBits9 = nBits8 - 8
nBits8 = 8
}
nBits7 = 8
}
nBits6 = 8
}
nBits5 = 8
}
b1 := r.mustReadNBitsInCurrentByte(nBits1)
b2, err := r.ReadNBitsAsUint8(nBits2)
if err != nil {
return 0, err
}
b3, err := r.ReadNBitsAsUint8(nBits3)
if err != nil {
return 0, err
}
b4, err := r.ReadNBitsAsUint8(nBits4)
if err != nil {
return 0, err
}
b5, err := r.ReadNBitsAsUint8(nBits5)
if err != nil {
return 0, err
}
b6, err := r.ReadNBitsAsUint8(nBits6)
if err != nil {
return 0, err
}
b7, err := r.ReadNBitsAsUint8(nBits7)
if err != nil {
return 0, err
}
b8, err := r.ReadNBitsAsUint8(nBits8)
if err != nil {
return 0, err
}
b9, err := r.ReadNBitsAsUint8(nBits9)
if err != nil {
return 0, err
}
return (uint64(b1) << (nBits2 + nBits3 + nBits4 + nBits5 + nBits6 + nBits7 + nBits8 + nBits9)) |
(uint64(b2) << (nBits3 + nBits4 + nBits5 + nBits6 + nBits7 + nBits8 + nBits9)) |
(uint64(b3) << (nBits4 + nBits5 + nBits6 + nBits7 + nBits8 + nBits9)) |
(uint64(b4) << (nBits5 + nBits6 + nBits7 + nBits8 + nBits9)) |
(uint64(b5) << (nBits6 + nBits7 + nBits8 + nBits9)) |
(uint64(b6) << (nBits7 + nBits8 + nBits9)) |
(uint64(b7) << (nBits8 + nBits9)) |
(uint64(b8) << (nBits9)) |
uint64(b9), nil
}
// ReadUint64BE reads 64 bits as a big endian unsigned integer from the bit stream and returns it in uint64.
func (r *Reader) ReadUint64BE() (uint64, error) {
return r.ReadNBitsAsUint64BE(64)
}
// ReadOptions is a set of options to read bits from the bit stream.
type ReadOptions struct {
AlignRight bool // If true, returned value will be aligned to right (default: align to left)
PadOne bool // If true, returned value will be padded with '1' instead of '0' (default: pad with '0')
}
// ReadNBits reads `nBits` bits from the bit stream and returns it as a slice of bytes.
// If `nBits` == 0, this function always returns nil.
func (r *Reader) ReadNBits(nBits uint8, opt *ReadOptions) ([]byte, error) {
if nBits == 0 {
return nil, nil
}
err := r.fillBufIfNeeded()
if err != nil {
return nil, err
}
padOne := (opt != nil && opt.PadOne)
alignRight := (opt != nil && opt.AlignRight)
maxByteLen := (nBits / 8) + 1
result := make([]byte, 0, maxByteLen)
// remaining bits in current byte
rb := r.currBitIndex + 1
var bitsToRead uint8
if nBits <= rb {
bitsToRead = nBits
} else {
bitsToRead = rb
}
tempByte := r.mustReadNBitsInCurrentByte(bitsToRead)
tempByte = tempByte << (8 - bitsToRead) // left align
tempBit := bitsToRead
nBits -= bitsToRead
if tempBit == 8 {
result = append(result, tempByte)
tempByte = 0
tempBit = 0
}
for nBits >= 8 {
err := r.fillBufIfNeeded()
if err != nil {
return nil, err
}
bitsToRead = 8
b := r.mustReadNBitsInCurrentByte(bitsToRead)
b1 := b >> tempBit
b2 := b << (8 - tempBit)
tempByte = tempByte | b1
result = append(result, tempByte)
tempByte = b2
nBits -= 8
}
if nBits > 0 {
err := r.fillBufIfNeeded()
if err != nil {
return nil, err
}
bitsToRead = nBits
b := r.mustReadNBitsInCurrentByte(bitsToRead)
b1 := b >> (bitsToRead - (8 - tempBit)) // wants to have (8 - tempBit) bits from b. b has bitsToRead bits
b2 := b << (8 - (bitsToRead - (8 - tempBit))) // wants to have (bitsToRead - <bits of b1>) left aligned.
tempByte = tempByte | b1
result = append(result, tempByte)
if nBits > (8 - tempBit) {
if padOne {
b2 = b2 | (0xff >> tempBit)
}
result = append(result, b2)
}
} else {
if tempBit > 0 {
result = append(result, tempByte)
}
}
if alignRight {
return nil, errors.New("not implemented yet")
}
return result, nil
}