bitreader.go 3.0 KB

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