// Copyright 2018 Klaus Post. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// Based on work Copyright (c) 2013, Yann Collet, released under BSD License.package huff0import ()// bitReader reads a bitstream in reverse.// The last set bit indicates the start of the stream and is used// for aligning the input.type bitReaderBytes struct { in []byte off uint// next byte to read is at in[off - 1] value uint64 bitsRead uint8}// init initializes and resets the bit reader.func ( *bitReaderBytes) ( []byte) error {iflen() < 1 {returnerrors.New("corrupt stream: too short") } .in = .off = uint(len())// The highest bit of the last byte indicates where to start := [len()-1]if == 0 {returnerrors.New("corrupt stream, did not find end of stream") } .bitsRead = 64 .value = 0iflen() >= 8 { .fillFastStart() } else { .fill() .fill() } .advance(8 - uint8(highBit32(uint32())))returnnil}// peekByteFast requires that at least one byte is requested every time.// There are no checks if the buffer is filled.func ( *bitReaderBytes) () uint8 { := uint8(.value >> 56)return}func ( *bitReaderBytes) ( uint8) { .bitsRead += .value <<= & 63}// fillFast() will make sure at least 32 bits are available.// There must be at least 4 bytes available.func ( *bitReaderBytes) () {if .bitsRead < 32 {return }// 2 bounds checks. := le.Load32(.in, .off-4) .value |= uint64() << (.bitsRead - 32) .bitsRead -= 32 .off -= 4}// fillFastStart() assumes the bitReaderBytes is empty and there is at least 8 bytes to read.func ( *bitReaderBytes) () {// Do single re-slice to avoid bounds checks. .value = le.Load64(.in, .off-8) .bitsRead = 0 .off -= 8}// fill() will make sure at least 32 bits are available.func ( *bitReaderBytes) () {if .bitsRead < 32 {return }if .off >= 4 { := le.Load32(.in, .off-4) .value |= uint64() << (.bitsRead - 32) .bitsRead -= 32 .off -= 4return }for .off > 0 { .value |= uint64(.in[.off-1]) << (.bitsRead - 8) .bitsRead -= 8 .off-- }}// finished returns true if all bits have been read from the bit stream.func ( *bitReaderBytes) () bool {return .off == 0 && .bitsRead >= 64}func ( *bitReaderBytes) () uint {return .off*8 + uint(64-.bitsRead)}// close the bitstream and returns an error if out-of-buffer reads occurred.func ( *bitReaderBytes) () error {// Release reference. .in = nilif .remaining() > 0 {returnfmt.Errorf("corrupt input: %d bits remain on stream", .remaining()) }if .bitsRead > 64 {returnio.ErrUnexpectedEOF }returnnil}// bitReaderShifted reads a bitstream in reverse.// The last set bit indicates the start of the stream and is used// for aligning the input.type bitReaderShifted struct { in []byte off uint// next byte to read is at in[off - 1] value uint64 bitsRead uint8}// init initializes and resets the bit reader.func ( *bitReaderShifted) ( []byte) error {iflen() < 1 {returnerrors.New("corrupt stream: too short") } .in = .off = uint(len())// The highest bit of the last byte indicates where to start := [len()-1]if == 0 {returnerrors.New("corrupt stream, did not find end of stream") } .bitsRead = 64 .value = 0iflen() >= 8 { .fillFastStart() } else { .fill() .fill() } .advance(8 - uint8(highBit32(uint32())))returnnil}// peekBitsFast requires that at least one bit is requested every time.// There are no checks if the buffer is filled.func ( *bitReaderShifted) ( uint8) uint16 {returnuint16(.value >> ((64 - ) & 63))}func ( *bitReaderShifted) ( uint8) { .bitsRead += .value <<= & 63}// fillFast() will make sure at least 32 bits are available.// There must be at least 4 bytes available.func ( *bitReaderShifted) () {if .bitsRead < 32 {return } := le.Load32(.in, .off-4) .value |= uint64() << ((.bitsRead - 32) & 63) .bitsRead -= 32 .off -= 4}// fillFastStart() assumes the bitReaderShifted is empty and there is at least 8 bytes to read.func ( *bitReaderShifted) () { .value = le.Load64(.in, .off-8) .bitsRead = 0 .off -= 8}// fill() will make sure at least 32 bits are available.func ( *bitReaderShifted) () {if .bitsRead < 32 {return }if .off > 4 { := le.Load32(.in, .off-4) .value |= uint64() << ((.bitsRead - 32) & 63) .bitsRead -= 32 .off -= 4return }for .off > 0 { .value |= uint64(.in[.off-1]) << ((.bitsRead - 8) & 63) .bitsRead -= 8 .off-- }}func ( *bitReaderShifted) () uint {return .off*8 + uint(64-.bitsRead)}// close the bitstream and returns an error if out-of-buffer reads occurred.func ( *bitReaderShifted) () error {// Release reference. .in = nilif .remaining() > 0 {returnfmt.Errorf("corrupt input: %d bits remain on stream", .remaining()) }if .bitsRead > 64 {returnio.ErrUnexpectedEOF }returnnil}
The pages are generated with Goldsv0.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.