bitreader.go 3.2 KB

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