cubefs
1// Copyright 2019+ Klaus Post. All rights reserved.
2// License information can be found in the LICENSE file.
3// Based on work by Yann Collet, released under BSD License.
4
5package zstd6
7import (8"encoding/binary"9"errors"10"fmt"11"io"12"math/bits"13)
14
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.
18type bitReader struct {19in []byte20off uint // next byte to read is at in[off - 1]21value uint64 // Maybe use [16]byte, but shifting is awkward.22bitsRead uint823}
24
25// init initializes and resets the bit reader.
26func (b *bitReader) init(in []byte) error {27if len(in) < 1 {28return errors.New("corrupt stream: too short")29}30b.in = in31b.off = uint(len(in))32// The highest bit of the last byte indicates where to start33v := in[len(in)-1]34if v == 0 {35return errors.New("corrupt stream, did not find end of stream")36}37b.bitsRead = 6438b.value = 039if len(in) >= 8 {40b.fillFastStart()41} else {42b.fill()43b.fill()44}45b.bitsRead += 8 - uint8(highBits(uint32(v)))46return nil47}
48
49// getBits will return n bits. n can be 0.
50func (b *bitReader) getBits(n uint8) int {51if n == 0 /*|| b.bitsRead >= 64 */ {52return 053}54return int(b.get32BitsFast(n))55}
56
57// get32BitsFast requires that at least one bit is requested every time.
58// There are no checks if the buffer is filled.
59func (b *bitReader) get32BitsFast(n uint8) uint32 {60const regMask = 64 - 161v := uint32((b.value << (b.bitsRead & regMask)) >> ((regMask + 1 - n) & regMask))62b.bitsRead += n63return v64}
65
66func (b *bitReader) get16BitsFast(n uint8) uint16 {67const regMask = 64 - 168v := uint16((b.value << (b.bitsRead & regMask)) >> ((regMask + 1 - n) & regMask))69b.bitsRead += n70return v71}
72
73// fillFast() will make sure at least 32 bits are available.
74// There must be at least 4 bytes available.
75func (b *bitReader) fillFast() {76if b.bitsRead < 32 {77return78}79// 2 bounds checks.80v := b.in[b.off-4:]81v = v[:4]82low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)83b.value = (b.value << 32) | uint64(low)84b.bitsRead -= 3285b.off -= 486}
87
88// fillFastStart() assumes the bitreader is empty and there is at least 8 bytes to read.
89func (b *bitReader) fillFastStart() {90// Do single re-slice to avoid bounds checks.91b.value = binary.LittleEndian.Uint64(b.in[b.off-8:])92b.bitsRead = 093b.off -= 894}
95
96// fill() will make sure at least 32 bits are available.
97func (b *bitReader) fill() {98if b.bitsRead < 32 {99return100}101if b.off >= 4 {102v := b.in[b.off-4:]103v = v[:4]104low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)105b.value = (b.value << 32) | uint64(low)106b.bitsRead -= 32107b.off -= 4108return109}110for b.off > 0 {111b.value = (b.value << 8) | uint64(b.in[b.off-1])112b.bitsRead -= 8113b.off--114}115}
116
117// finished returns true if all bits have been read from the bit stream.
118func (b *bitReader) finished() bool {119return b.off == 0 && b.bitsRead >= 64120}
121
122// overread returns true if more bits have been requested than is on the stream.
123func (b *bitReader) overread() bool {124return b.bitsRead > 64125}
126
127// remain returns the number of bits remaining.
128func (b *bitReader) remain() uint {129return b.off*8 + 64 - uint(b.bitsRead)130}
131
132// close the bitstream and returns an error if out-of-buffer reads occurred.
133func (b *bitReader) close() error {134// Release reference.135b.in = nil136if !b.finished() {137return fmt.Errorf("%d extra bits on block, should be 0", b.remain())138}139if b.bitsRead > 64 {140return io.ErrUnexpectedEOF141}142return nil143}
144
145func highBits(val uint32) (n uint32) {146return uint32(bits.Len32(val) - 1)147}
148