bitreader.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2018 Klaus Post. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Based on work Copyright (c) 2013, Yann Collet, released under BSD License.
  5. package fse
  6. import (
  7. "errors"
  8. "io"
  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
  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) uint16 {
  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) uint16 {
  48. const regMask = 64 - 1
  49. v := uint16((b.value << (b.bitsRead & regMask)) >> ((regMask + 1 - n) & regMask))
  50. b.bitsRead += n
  51. return 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. // close the bitstream and returns an error if out-of-buffer reads occurred.
  90. func (b *bitReader) close() error {
  91. // Release reference.
  92. b.in = nil
  93. if b.bitsRead > 64 {
  94. return io.ErrUnexpectedEOF
  95. }
  96. return nil
  97. }