bitreader.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. "io"
  10. )
  11. // bitReader reads a bitstream in reverse.
  12. // The last set bit indicates the start of the stream and is used
  13. // for aligning the input.
  14. type bitReader struct {
  15. in []byte
  16. off uint // next byte to read is at in[off - 1]
  17. value uint64
  18. bitsRead uint8
  19. }
  20. // init initializes and resets the bit reader.
  21. func (b *bitReader) init(in []byte) error {
  22. if len(in) < 1 {
  23. return errors.New("corrupt stream: too short")
  24. }
  25. b.in = in
  26. b.off = uint(len(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.bitsRead = 64
  33. b.value = 0
  34. if len(in) >= 8 {
  35. b.fillFastStart()
  36. } else {
  37. b.fill()
  38. b.fill()
  39. }
  40. b.bitsRead += 8 - uint8(highBit32(uint32(v)))
  41. return nil
  42. }
  43. // peekBitsFast requires that at least one bit is requested every time.
  44. // There are no checks if the buffer is filled.
  45. func (b *bitReader) peekBitsFast(n uint8) uint16 {
  46. const regMask = 64 - 1
  47. v := uint16((b.value << (b.bitsRead & regMask)) >> ((regMask + 1 - n) & regMask))
  48. return v
  49. }
  50. // fillFast() will make sure at least 32 bits are available.
  51. // There must be at least 4 bytes available.
  52. func (b *bitReader) fillFast() {
  53. if b.bitsRead < 32 {
  54. return
  55. }
  56. // 2 bounds checks.
  57. v := b.in[b.off-4 : b.off]
  58. v = v[:4]
  59. low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
  60. b.value = (b.value << 32) | uint64(low)
  61. b.bitsRead -= 32
  62. b.off -= 4
  63. }
  64. func (b *bitReader) advance(n uint8) {
  65. b.bitsRead += n
  66. }
  67. // fillFastStart() assumes the bitreader is empty and there is at least 8 bytes to read.
  68. func (b *bitReader) 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 *bitReader) fill() {
  76. if b.bitsRead < 32 {
  77. return
  78. }
  79. if b.off > 4 {
  80. v := b.in[b.off-4:]
  81. v = v[:4]
  82. low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
  83. b.value = (b.value << 32) | uint64(low)
  84. b.bitsRead -= 32
  85. b.off -= 4
  86. return
  87. }
  88. for b.off > 0 {
  89. b.value = (b.value << 8) | uint64(b.in[b.off-1])
  90. b.bitsRead -= 8
  91. b.off--
  92. }
  93. }
  94. // finished returns true if all bits have been read from the bit stream.
  95. func (b *bitReader) finished() bool {
  96. return b.off == 0 && b.bitsRead >= 64
  97. }
  98. // close the bitstream and returns an error if out-of-buffer reads occurred.
  99. func (b *bitReader) close() error {
  100. // Release reference.
  101. b.in = nil
  102. if b.bitsRead > 64 {
  103. return io.ErrUnexpectedEOF
  104. }
  105. return nil
  106. }
  107. // bitReader reads a bitstream in reverse.
  108. // The last set bit indicates the start of the stream and is used
  109. // for aligning the input.
  110. type bitReaderBytes struct {
  111. in []byte
  112. off uint // next byte to read is at in[off - 1]
  113. value uint64
  114. bitsRead uint8
  115. }
  116. // init initializes and resets the bit reader.
  117. func (b *bitReaderBytes) init(in []byte) error {
  118. if len(in) < 1 {
  119. return errors.New("corrupt stream: too short")
  120. }
  121. b.in = in
  122. b.off = uint(len(in))
  123. // The highest bit of the last byte indicates where to start
  124. v := in[len(in)-1]
  125. if v == 0 {
  126. return errors.New("corrupt stream, did not find end of stream")
  127. }
  128. b.bitsRead = 64
  129. b.value = 0
  130. if len(in) >= 8 {
  131. b.fillFastStart()
  132. } else {
  133. b.fill()
  134. b.fill()
  135. }
  136. b.advance(8 - uint8(highBit32(uint32(v))))
  137. return nil
  138. }
  139. // peekBitsFast requires that at least one bit is requested every time.
  140. // There are no checks if the buffer is filled.
  141. func (b *bitReaderBytes) peekByteFast() uint8 {
  142. got := uint8(b.value >> 56)
  143. return got
  144. }
  145. func (b *bitReaderBytes) advance(n uint8) {
  146. b.bitsRead += n
  147. b.value <<= n & 63
  148. }
  149. // fillFast() will make sure at least 32 bits are available.
  150. // There must be at least 4 bytes available.
  151. func (b *bitReaderBytes) fillFast() {
  152. if b.bitsRead < 32 {
  153. return
  154. }
  155. // 2 bounds checks.
  156. v := b.in[b.off-4 : b.off]
  157. v = v[:4]
  158. low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
  159. b.value |= uint64(low) << (b.bitsRead - 32)
  160. b.bitsRead -= 32
  161. b.off -= 4
  162. }
  163. // fillFastStart() assumes the bitReaderBytes is empty and there is at least 8 bytes to read.
  164. func (b *bitReaderBytes) fillFastStart() {
  165. // Do single re-slice to avoid bounds checks.
  166. b.value = binary.LittleEndian.Uint64(b.in[b.off-8:])
  167. b.bitsRead = 0
  168. b.off -= 8
  169. }
  170. // fill() will make sure at least 32 bits are available.
  171. func (b *bitReaderBytes) fill() {
  172. if b.bitsRead < 32 {
  173. return
  174. }
  175. if b.off > 4 {
  176. v := b.in[b.off-4:]
  177. v = v[:4]
  178. low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
  179. b.value |= uint64(low) << (b.bitsRead - 32)
  180. b.bitsRead -= 32
  181. b.off -= 4
  182. return
  183. }
  184. for b.off > 0 {
  185. b.value |= uint64(b.in[b.off-1]) << (b.bitsRead - 8)
  186. b.bitsRead -= 8
  187. b.off--
  188. }
  189. }
  190. // finished returns true if all bits have been read from the bit stream.
  191. func (b *bitReaderBytes) finished() bool {
  192. return b.off == 0 && b.bitsRead >= 64
  193. }
  194. // close the bitstream and returns an error if out-of-buffer reads occurred.
  195. func (b *bitReaderBytes) close() error {
  196. // Release reference.
  197. b.in = nil
  198. if b.bitsRead > 64 {
  199. return io.ErrUnexpectedEOF
  200. }
  201. return nil
  202. }
  203. // bitReaderShifted reads a bitstream in reverse.
  204. // The last set bit indicates the start of the stream and is used
  205. // for aligning the input.
  206. type bitReaderShifted struct {
  207. in []byte
  208. off uint // next byte to read is at in[off - 1]
  209. value uint64
  210. bitsRead uint8
  211. }
  212. // init initializes and resets the bit reader.
  213. func (b *bitReaderShifted) init(in []byte) error {
  214. if len(in) < 1 {
  215. return errors.New("corrupt stream: too short")
  216. }
  217. b.in = in
  218. b.off = uint(len(in))
  219. // The highest bit of the last byte indicates where to start
  220. v := in[len(in)-1]
  221. if v == 0 {
  222. return errors.New("corrupt stream, did not find end of stream")
  223. }
  224. b.bitsRead = 64
  225. b.value = 0
  226. if len(in) >= 8 {
  227. b.fillFastStart()
  228. } else {
  229. b.fill()
  230. b.fill()
  231. }
  232. b.advance(8 - uint8(highBit32(uint32(v))))
  233. return nil
  234. }
  235. // peekBitsFast requires that at least one bit is requested every time.
  236. // There are no checks if the buffer is filled.
  237. func (b *bitReaderShifted) peekBitsFast(n uint8) uint16 {
  238. return uint16(b.value >> ((64 - n) & 63))
  239. }
  240. func (b *bitReaderShifted) advance(n uint8) {
  241. b.bitsRead += n
  242. b.value <<= n & 63
  243. }
  244. // fillFast() will make sure at least 32 bits are available.
  245. // There must be at least 4 bytes available.
  246. func (b *bitReaderShifted) fillFast() {
  247. if b.bitsRead < 32 {
  248. return
  249. }
  250. // 2 bounds checks.
  251. v := b.in[b.off-4 : b.off]
  252. v = v[:4]
  253. low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
  254. b.value |= uint64(low) << ((b.bitsRead - 32) & 63)
  255. b.bitsRead -= 32
  256. b.off -= 4
  257. }
  258. // fillFastStart() assumes the bitReaderShifted is empty and there is at least 8 bytes to read.
  259. func (b *bitReaderShifted) fillFastStart() {
  260. // Do single re-slice to avoid bounds checks.
  261. b.value = binary.LittleEndian.Uint64(b.in[b.off-8:])
  262. b.bitsRead = 0
  263. b.off -= 8
  264. }
  265. // fill() will make sure at least 32 bits are available.
  266. func (b *bitReaderShifted) fill() {
  267. if b.bitsRead < 32 {
  268. return
  269. }
  270. if b.off > 4 {
  271. v := b.in[b.off-4:]
  272. v = v[:4]
  273. low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
  274. b.value |= uint64(low) << ((b.bitsRead - 32) & 63)
  275. b.bitsRead -= 32
  276. b.off -= 4
  277. return
  278. }
  279. for b.off > 0 {
  280. b.value |= uint64(b.in[b.off-1]) << ((b.bitsRead - 8) & 63)
  281. b.bitsRead -= 8
  282. b.off--
  283. }
  284. }
  285. // finished returns true if all bits have been read from the bit stream.
  286. func (b *bitReaderShifted) finished() bool {
  287. return b.off == 0 && b.bitsRead >= 64
  288. }
  289. // close the bitstream and returns an error if out-of-buffer reads occurred.
  290. func (b *bitReaderShifted) close() error {
  291. // Release reference.
  292. b.in = nil
  293. if b.bitsRead > 64 {
  294. return io.ErrUnexpectedEOF
  295. }
  296. return nil
  297. }