Source File
bitreader.go
Belonging Package
github.com/klauspost/compress/zstd
// Copyright 2019+ Klaus Post. All rights reserved.// License information can be found in the LICENSE file.// Based on work by Yann Collet, released under BSD License.package zstdimport ()// bitReader reads a bitstream in reverse.// The last set bit indicates the start of the stream and is used// for aligning the input.type bitReader struct {in []bytevalue uint64 // Maybe use [16]byte, but shifting is awkward.cursor int // offset where next read should endbitsRead uint8}// init initializes and resets the bit reader.func ( *bitReader) ( []byte) error {if len() < 1 {return errors.New("corrupt stream: too short")}.in =// The highest bit of the last byte indicates where to start:= [len()-1]if == 0 {return errors.New("corrupt stream, did not find end of stream")}.cursor = len().bitsRead = 64.value = 0if len() >= 8 {.fillFastStart()} else {.fill().fill()}.bitsRead += 8 - uint8(highBits(uint32()))return nil}// getBits will return n bits. n can be 0.func ( *bitReader) ( uint8) int {if == 0 /*|| b.bitsRead >= 64 */ {return 0}return int(.get32BitsFast())}// get32BitsFast requires that at least one bit is requested every time.// There are no checks if the buffer is filled.func ( *bitReader) ( uint8) uint32 {const = 64 - 1:= uint32((.value << (.bitsRead & )) >> (( + 1 - ) & )).bitsRead +=return}// fillFast() will make sure at least 32 bits are available.// There must be at least 4 bytes available.func ( *bitReader) () {if .bitsRead < 32 {return}.cursor -= 4.value = (.value << 32) | uint64(le.Load32(.in, .cursor)).bitsRead -= 32}// fillFastStart() assumes the bitreader is empty and there is at least 8 bytes to read.func ( *bitReader) () {.cursor -= 8.value = le.Load64(.in, .cursor).bitsRead = 0}// fill() will make sure at least 32 bits are available.func ( *bitReader) () {if .bitsRead < 32 {return}if .cursor >= 4 {.cursor -= 4.value = (.value << 32) | uint64(le.Load32(.in, .cursor)).bitsRead -= 32return}.bitsRead -= uint8(8 * .cursor)for .cursor > 0 {.cursor -= 1.value = (.value << 8) | uint64(.in[.cursor])}}// finished returns true if all bits have been read from the bit stream.func ( *bitReader) () bool {return .cursor == 0 && .bitsRead >= 64}// overread returns true if more bits have been requested than is on the stream.func ( *bitReader) () bool {return .bitsRead > 64}// remain returns the number of bits remaining.func ( *bitReader) () uint {return 8*uint(.cursor) + 64 - uint(.bitsRead)}// close the bitstream and returns an error if out-of-buffer reads occurred.func ( *bitReader) () error {// Release reference..in = nil.cursor = 0if !.finished() {return fmt.Errorf("%d extra bits on block, should be 0", .remain())}if .bitsRead > 64 {return io.ErrUnexpectedEOF}return nil}func highBits( uint32) ( uint32) {return uint32(bits.Len32() - 1)}
![]() |
The pages are generated with Golds v0.8.2. (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. |