bitreader.go 5.6 KB

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