blob: 753d17df634cd83a6a36fbce7c22dc9c69fd3879 [file] [log] [blame]
kesavandc71914f2022-03-25 11:19:03 +05301// 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 zstd
6
7import (
8 "encoding/binary"
9 "errors"
10 "io"
11 "math/bits"
12)
13
14// bitReader reads a bitstream in reverse.
15// The last set bit indicates the start of the stream and is used
16// for aligning the input.
17type bitReader struct {
18 in []byte
19 off uint // next byte to read is at in[off - 1]
20 value uint64 // Maybe use [16]byte, but shifting is awkward.
21 bitsRead uint8
22}
23
24// init initializes and resets the bit reader.
25func (b *bitReader) init(in []byte) error {
26 if len(in) < 1 {
27 return errors.New("corrupt stream: too short")
28 }
29 b.in = in
30 b.off = uint(len(in))
31 // The highest bit of the last byte indicates where to start
32 v := in[len(in)-1]
33 if v == 0 {
34 return errors.New("corrupt stream, did not find end of stream")
35 }
36 b.bitsRead = 64
37 b.value = 0
38 if len(in) >= 8 {
39 b.fillFastStart()
40 } else {
41 b.fill()
42 b.fill()
43 }
44 b.bitsRead += 8 - uint8(highBits(uint32(v)))
45 return nil
46}
47
48// getBits will return n bits. n can be 0.
49func (b *bitReader) getBits(n uint8) int {
50 if n == 0 /*|| b.bitsRead >= 64 */ {
51 return 0
52 }
53 return int(b.get32BitsFast(n))
54}
55
56// get32BitsFast requires that at least one bit is requested every time.
57// There are no checks if the buffer is filled.
58func (b *bitReader) get32BitsFast(n uint8) uint32 {
59 const regMask = 64 - 1
60 v := uint32((b.value << (b.bitsRead & regMask)) >> ((regMask + 1 - n) & regMask))
61 b.bitsRead += n
62 return v
63}
64
65func (b *bitReader) get16BitsFast(n uint8) uint16 {
66 const regMask = 64 - 1
67 v := uint16((b.value << (b.bitsRead & regMask)) >> ((regMask + 1 - n) & regMask))
68 b.bitsRead += n
69 return v
70}
71
72// fillFast() will make sure at least 32 bits are available.
73// There must be at least 4 bytes available.
74func (b *bitReader) fillFast() {
75 if b.bitsRead < 32 {
76 return
77 }
78 // 2 bounds checks.
79 v := b.in[b.off-4:]
80 v = v[:4]
81 low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
82 b.value = (b.value << 32) | uint64(low)
83 b.bitsRead -= 32
84 b.off -= 4
85}
86
87// fillFastStart() assumes the bitreader is empty and there is at least 8 bytes to read.
88func (b *bitReader) fillFastStart() {
89 // Do single re-slice to avoid bounds checks.
90 b.value = binary.LittleEndian.Uint64(b.in[b.off-8:])
91 b.bitsRead = 0
92 b.off -= 8
93}
94
95// fill() will make sure at least 32 bits are available.
96func (b *bitReader) fill() {
97 if b.bitsRead < 32 {
98 return
99 }
100 if b.off >= 4 {
101 v := b.in[b.off-4:]
102 v = v[:4]
103 low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
104 b.value = (b.value << 32) | uint64(low)
105 b.bitsRead -= 32
106 b.off -= 4
107 return
108 }
109 for b.off > 0 {
110 b.value = (b.value << 8) | uint64(b.in[b.off-1])
111 b.bitsRead -= 8
112 b.off--
113 }
114}
115
116// finished returns true if all bits have been read from the bit stream.
117func (b *bitReader) finished() bool {
118 return b.off == 0 && b.bitsRead >= 64
119}
120
121// overread returns true if more bits have been requested than is on the stream.
122func (b *bitReader) overread() bool {
123 return b.bitsRead > 64
124}
125
126// remain returns the number of bits remaining.
127func (b *bitReader) remain() uint {
128 return b.off*8 + 64 - uint(b.bitsRead)
129}
130
131// close the bitstream and returns an error if out-of-buffer reads occurred.
132func (b *bitReader) close() error {
133 // Release reference.
134 b.in = nil
135 if b.bitsRead > 64 {
136 return io.ErrUnexpectedEOF
137 }
138 return nil
139}
140
141func highBits(val uint32) (n uint32) {
142 return uint32(bits.Len32(val) - 1)
143}