bitreader.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 huff0
  6. import (
  7. "errors"
  8. "fmt"
  9. "io"
  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 bitReaderBytes struct {
  16. in []byte
  17. off uint // next byte to read is at in[off - 1]
  18. value uint64
  19. bitsRead uint8
  20. }
  21. // init initializes and resets the bit reader.
  22. func (b *bitReaderBytes) init(in []byte) error {
  23. if len(in) < 1 {
  24. return errors.New("corrupt stream: too short")
  25. }
  26. b.in = in
  27. b.off = uint(len(in))
  28. // The highest bit of the last byte indicates where to start
  29. v := in[len(in)-1]
  30. if v == 0 {
  31. return errors.New("corrupt stream, did not find end of stream")
  32. }
  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.advance(8 - uint8(highBit32(uint32(v))))
  42. return nil
  43. }
  44. // peekByteFast requires that at least one byte is requested every time.
  45. // There are no checks if the buffer is filled.
  46. func (b *bitReaderBytes) peekByteFast() uint8 {
  47. got := uint8(b.value >> 56)
  48. return got
  49. }
  50. func (b *bitReaderBytes) advance(n uint8) {
  51. b.bitsRead += n
  52. b.value <<= n & 63
  53. }
  54. // fillFast() will make sure at least 32 bits are available.
  55. // There must be at least 4 bytes available.
  56. func (b *bitReaderBytes) fillFast() {
  57. if b.bitsRead < 32 {
  58. return
  59. }
  60. // 2 bounds checks.
  61. low := le.Load32(b.in, b.off-4)
  62. b.value |= uint64(low) << (b.bitsRead - 32)
  63. b.bitsRead -= 32
  64. b.off -= 4
  65. }
  66. // fillFastStart() assumes the bitReaderBytes is empty and there is at least 8 bytes to read.
  67. func (b *bitReaderBytes) fillFastStart() {
  68. // Do single re-slice to avoid bounds checks.
  69. b.value = le.Load64(b.in, b.off-8)
  70. b.bitsRead = 0
  71. b.off -= 8
  72. }
  73. // fill() will make sure at least 32 bits are available.
  74. func (b *bitReaderBytes) fill() {
  75. if b.bitsRead < 32 {
  76. return
  77. }
  78. if b.off >= 4 {
  79. low := le.Load32(b.in, b.off-4)
  80. b.value |= uint64(low) << (b.bitsRead - 32)
  81. b.bitsRead -= 32
  82. b.off -= 4
  83. return
  84. }
  85. for b.off > 0 {
  86. b.value |= uint64(b.in[b.off-1]) << (b.bitsRead - 8)
  87. b.bitsRead -= 8
  88. b.off--
  89. }
  90. }
  91. // finished returns true if all bits have been read from the bit stream.
  92. func (b *bitReaderBytes) finished() bool {
  93. return b.off == 0 && b.bitsRead >= 64
  94. }
  95. func (b *bitReaderBytes) remaining() uint {
  96. return b.off*8 + uint(64-b.bitsRead)
  97. }
  98. // close the bitstream and returns an error if out-of-buffer reads occurred.
  99. func (b *bitReaderBytes) close() error {
  100. // Release reference.
  101. b.in = nil
  102. if b.remaining() > 0 {
  103. return fmt.Errorf("corrupt input: %d bits remain on stream", b.remaining())
  104. }
  105. if b.bitsRead > 64 {
  106. return io.ErrUnexpectedEOF
  107. }
  108. return nil
  109. }
  110. // bitReaderShifted reads a bitstream in reverse.
  111. // The last set bit indicates the start of the stream and is used
  112. // for aligning the input.
  113. type bitReaderShifted struct {
  114. in []byte
  115. off uint // next byte to read is at in[off - 1]
  116. value uint64
  117. bitsRead uint8
  118. }
  119. // init initializes and resets the bit reader.
  120. func (b *bitReaderShifted) init(in []byte) error {
  121. if len(in) < 1 {
  122. return errors.New("corrupt stream: too short")
  123. }
  124. b.in = in
  125. b.off = uint(len(in))
  126. // The highest bit of the last byte indicates where to start
  127. v := in[len(in)-1]
  128. if v == 0 {
  129. return errors.New("corrupt stream, did not find end of stream")
  130. }
  131. b.bitsRead = 64
  132. b.value = 0
  133. if len(in) >= 8 {
  134. b.fillFastStart()
  135. } else {
  136. b.fill()
  137. b.fill()
  138. }
  139. b.advance(8 - uint8(highBit32(uint32(v))))
  140. return nil
  141. }
  142. // peekBitsFast requires that at least one bit is requested every time.
  143. // There are no checks if the buffer is filled.
  144. func (b *bitReaderShifted) peekBitsFast(n uint8) uint16 {
  145. return uint16(b.value >> ((64 - n) & 63))
  146. }
  147. func (b *bitReaderShifted) advance(n uint8) {
  148. b.bitsRead += n
  149. b.value <<= n & 63
  150. }
  151. // fillFast() will make sure at least 32 bits are available.
  152. // There must be at least 4 bytes available.
  153. func (b *bitReaderShifted) fillFast() {
  154. if b.bitsRead < 32 {
  155. return
  156. }
  157. low := le.Load32(b.in, b.off-4)
  158. b.value |= uint64(low) << ((b.bitsRead - 32) & 63)
  159. b.bitsRead -= 32
  160. b.off -= 4
  161. }
  162. // fillFastStart() assumes the bitReaderShifted is empty and there is at least 8 bytes to read.
  163. func (b *bitReaderShifted) fillFastStart() {
  164. b.value = le.Load64(b.in, b.off-8)
  165. b.bitsRead = 0
  166. b.off -= 8
  167. }
  168. // fill() will make sure at least 32 bits are available.
  169. func (b *bitReaderShifted) fill() {
  170. if b.bitsRead < 32 {
  171. return
  172. }
  173. if b.off > 4 {
  174. low := le.Load32(b.in, b.off-4)
  175. b.value |= uint64(low) << ((b.bitsRead - 32) & 63)
  176. b.bitsRead -= 32
  177. b.off -= 4
  178. return
  179. }
  180. for b.off > 0 {
  181. b.value |= uint64(b.in[b.off-1]) << ((b.bitsRead - 8) & 63)
  182. b.bitsRead -= 8
  183. b.off--
  184. }
  185. }
  186. func (b *bitReaderShifted) remaining() uint {
  187. return b.off*8 + uint(64-b.bitsRead)
  188. }
  189. // close the bitstream and returns an error if out-of-buffer reads occurred.
  190. func (b *bitReaderShifted) close() error {
  191. // Release reference.
  192. b.in = nil
  193. if b.remaining() > 0 {
  194. return fmt.Errorf("corrupt input: %d bits remain on stream", b.remaining())
  195. }
  196. if b.bitsRead > 64 {
  197. return io.ErrUnexpectedEOF
  198. }
  199. return nil
  200. }