package zstd
import (
"errors"
"fmt"
"io"
"math/bits"
"github.com/klauspost/compress/internal/le"
"github.com/klauspost/compress/internal/regmask"
)
type bitReader struct {
in []byte
value uint64
cursor int
bitsRead uint8
}
func (b *bitReader ) init (in []byte ) error {
if len (in ) < 1 {
return errors .New ("corrupt stream: too short" )
}
b .in = in
v := in [len (in )-1 ]
if v == 0 {
return errors .New ("corrupt stream, did not find end of stream" )
}
b .cursor = len (in )
b .bitsRead = 64
b .value = 0
if len (in ) >= 8 {
b .fillFastStart ()
} else {
b .fill ()
b .fill ()
}
b .bitsRead += 8 - uint8 (highBits (uint32 (v )))
return nil
}
func (b *bitReader ) getBits (n uint8 ) int {
if n == 0 {
return 0
}
return int (b .get32BitsFast (n ))
}
func (b *bitReader ) get32BitsFast (n uint8 ) uint32 {
const regMask = 64 - 1
v := uint32 ((b .value << (b .bitsRead & regMask )) >> ((regMask + 1 - n ) & regMask ))
b .bitsRead += n
return v
}
func (b *bitReader ) fillFastStart () {
b .cursor -= 8
b .value = le .Load64 (b .in , b .cursor )
b .bitsRead = 0
}
func (b *bitReader ) fill () {
if b .bitsRead < 32 {
return
}
if b .cursor >= 4 {
b .cursor -= 4
b .value = (b .value << 32 ) | uint64 (le .Load32 (b .in , b .cursor ))
b .bitsRead -= 32
return
}
b .bitsRead -= uint8 (8 * b .cursor )
for b .cursor > 0 {
b .cursor -= 1
b .value = (b .value << 8 ) | uint64 (b .in [b .cursor ])
}
}
type bitReadState struct {
value uint64
cursor int
bitsRead uint8
}
func (b *bitReader ) state () bitReadState {
return bitReadState {value : b .value , cursor : b .cursor , bitsRead : b .bitsRead }
}
func (b *bitReader ) setState (s bitReadState ) {
b .value , b .cursor , b .bitsRead = s .value , s .cursor , s .bitsRead
}
func (s bitReadState ) getBits (n uint8 ) (bitReadState , int ) {
if n == 0 {
return s , 0
}
s , v := s .get32BitsFast (n )
return s , int (v )
}
func (s bitReadState ) get32BitsFast (n uint8 ) (bitReadState , uint32 ) {
v := uint32 ((s .value << (s .bitsRead & regmask .Shift64ByUint8 )) >> ((64 - n ) & regmask .Shift64ByUint8 ))
s .bitsRead += n
return s , v
}
func (s bitReadState ) fill (in []byte ) bitReadState {
if s .bitsRead < 32 {
return s
}
if s .cursor >= 4 {
s .cursor -= 4
s .value = (s .value << 32 ) | uint64 (le .Load32 (in , s .cursor ))
s .bitsRead -= 32
return s
}
s .bitsRead -= uint8 (8 * s .cursor )
for s .cursor > 0 {
s .cursor --
s .value = (s .value << 8 ) | uint64 (in [s .cursor ])
}
return s
}
func (b *bitReader ) finished () bool {
return b .cursor == 0 && b .bitsRead >= 64
}
func (b *bitReader ) overread () bool {
return b .bitsRead > 64
}
func (b *bitReader ) remain () uint {
return 8 *uint (b .cursor ) + 64 - uint (b .bitsRead )
}
func (b *bitReader ) close () error {
b .in = nil
b .cursor = 0
if !b .finished () {
return fmt .Errorf ("%d extra bits on block, should be 0" , b .remain ())
}
if b .bitsRead > 64 {
return io .ErrUnexpectedEOF
}
return nil
}
func highBits(val uint32 ) (n uint32 ) {
return uint32 (bits .Len32 (val ) - 1 )
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .