1
// Copyright 2018 Klaus Post. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
// Based on work Copyright (c) 2013, Yann Collet, released under BSD License.
15
// bitReader reads a bitstream in reverse.
16
// The last set bit indicates the start of the stream and is used
17
// for aligning the input.
18
type bitReaderBytes struct {
20
off uint // next byte to read is at in[off - 1]
25
// init initializes and resets the bit reader.
26
func (b *bitReaderBytes) init(in []byte) error {
28
return errors.New("corrupt stream: too short")
32
// The highest bit of the last byte indicates where to start
35
return errors.New("corrupt stream, did not find end of stream")
45
b.advance(8 - uint8(highBit32(uint32(v))))
49
// peekBitsFast requires that at least one bit is requested every time.
50
// There are no checks if the buffer is filled.
51
func (b *bitReaderBytes) peekByteFast() uint8 {
52
got := uint8(b.value >> 56)
56
func (b *bitReaderBytes) advance(n uint8) {
61
// fillFast() will make sure at least 32 bits are available.
62
// There must be at least 4 bytes available.
63
func (b *bitReaderBytes) fillFast() {
69
v := b.in[b.off-4 : b.off]
71
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
72
b.value |= uint64(low) << (b.bitsRead - 32)
77
// fillFastStart() assumes the bitReaderBytes is empty and there is at least 8 bytes to read.
78
func (b *bitReaderBytes) fillFastStart() {
79
// Do single re-slice to avoid bounds checks.
80
b.value = binary.LittleEndian.Uint64(b.in[b.off-8:])
85
// fill() will make sure at least 32 bits are available.
86
func (b *bitReaderBytes) fill() {
93
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
94
b.value |= uint64(low) << (b.bitsRead - 32)
100
b.value |= uint64(b.in[b.off-1]) << (b.bitsRead - 8)
106
// finished returns true if all bits have been read from the bit stream.
107
func (b *bitReaderBytes) finished() bool {
108
return b.off == 0 && b.bitsRead >= 64
111
func (b *bitReaderBytes) remaining() uint {
112
return b.off*8 + uint(64-b.bitsRead)
115
// close the bitstream and returns an error if out-of-buffer reads occurred.
116
func (b *bitReaderBytes) close() error {
117
// Release reference.
119
if b.remaining() > 0 {
120
return fmt.Errorf("corrupt input: %d bits remain on stream", b.remaining())
123
return io.ErrUnexpectedEOF
128
// bitReaderShifted reads a bitstream in reverse.
129
// The last set bit indicates the start of the stream and is used
130
// for aligning the input.
131
type bitReaderShifted struct {
133
off uint // next byte to read is at in[off - 1]
138
// init initializes and resets the bit reader.
139
func (b *bitReaderShifted) init(in []byte) error {
141
return errors.New("corrupt stream: too short")
144
b.off = uint(len(in))
145
// The highest bit of the last byte indicates where to start
148
return errors.New("corrupt stream, did not find end of stream")
158
b.advance(8 - uint8(highBit32(uint32(v))))
162
// peekBitsFast requires that at least one bit is requested every time.
163
// There are no checks if the buffer is filled.
164
func (b *bitReaderShifted) peekBitsFast(n uint8) uint16 {
165
return uint16(b.value >> ((64 - n) & 63))
168
func (b *bitReaderShifted) advance(n uint8) {
173
// fillFast() will make sure at least 32 bits are available.
174
// There must be at least 4 bytes available.
175
func (b *bitReaderShifted) fillFast() {
181
v := b.in[b.off-4 : b.off]
183
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
184
b.value |= uint64(low) << ((b.bitsRead - 32) & 63)
189
// fillFastStart() assumes the bitReaderShifted is empty and there is at least 8 bytes to read.
190
func (b *bitReaderShifted) fillFastStart() {
191
// Do single re-slice to avoid bounds checks.
192
b.value = binary.LittleEndian.Uint64(b.in[b.off-8:])
197
// fill() will make sure at least 32 bits are available.
198
func (b *bitReaderShifted) fill() {
205
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
206
b.value |= uint64(low) << ((b.bitsRead - 32) & 63)
212
b.value |= uint64(b.in[b.off-1]) << ((b.bitsRead - 8) & 63)
218
// finished returns true if all bits have been read from the bit stream.
219
func (b *bitReaderShifted) finished() bool {
220
return b.off == 0 && b.bitsRead >= 64
223
func (b *bitReaderShifted) remaining() uint {
224
return b.off*8 + uint(64-b.bitsRead)
227
// close the bitstream and returns an error if out-of-buffer reads occurred.
228
func (b *bitReaderShifted) close() error {
229
// Release reference.
231
if b.remaining() > 0 {
232
return fmt.Errorf("corrupt input: %d bits remain on stream", b.remaining())
235
return io.ErrUnexpectedEOF