Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

234 строки
5.7 KiB

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