bitreader.go 3.5 KB

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