bit_reader.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package brotli
  2. import "encoding/binary"
  3. /* Copyright 2013 Google Inc. All Rights Reserved.
  4. Distributed under MIT license.
  5. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  6. */
  7. /* Bit reading helpers */
  8. const shortFillBitWindowRead = (8 >> 1)
  9. var kBitMask = [33]uint32{
  10. 0x00000000,
  11. 0x00000001,
  12. 0x00000003,
  13. 0x00000007,
  14. 0x0000000F,
  15. 0x0000001F,
  16. 0x0000003F,
  17. 0x0000007F,
  18. 0x000000FF,
  19. 0x000001FF,
  20. 0x000003FF,
  21. 0x000007FF,
  22. 0x00000FFF,
  23. 0x00001FFF,
  24. 0x00003FFF,
  25. 0x00007FFF,
  26. 0x0000FFFF,
  27. 0x0001FFFF,
  28. 0x0003FFFF,
  29. 0x0007FFFF,
  30. 0x000FFFFF,
  31. 0x001FFFFF,
  32. 0x003FFFFF,
  33. 0x007FFFFF,
  34. 0x00FFFFFF,
  35. 0x01FFFFFF,
  36. 0x03FFFFFF,
  37. 0x07FFFFFF,
  38. 0x0FFFFFFF,
  39. 0x1FFFFFFF,
  40. 0x3FFFFFFF,
  41. 0x7FFFFFFF,
  42. 0xFFFFFFFF,
  43. }
  44. func bitMask(n uint32) uint32 {
  45. return kBitMask[n]
  46. }
  47. type bitReader struct {
  48. val_ uint64
  49. bit_pos_ uint32
  50. input []byte
  51. input_len uint
  52. byte_pos uint
  53. }
  54. type bitReaderState struct {
  55. val_ uint64
  56. bit_pos_ uint32
  57. input []byte
  58. input_len uint
  59. byte_pos uint
  60. }
  61. /* Initializes the BrotliBitReader fields. */
  62. /* Ensures that accumulator is not empty.
  63. May consume up to sizeof(brotli_reg_t) - 1 bytes of input.
  64. Returns false if data is required but there is no input available.
  65. For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned
  66. reading. */
  67. func bitReaderSaveState(from *bitReader, to *bitReaderState) {
  68. to.val_ = from.val_
  69. to.bit_pos_ = from.bit_pos_
  70. to.input = from.input
  71. to.input_len = from.input_len
  72. to.byte_pos = from.byte_pos
  73. }
  74. func bitReaderRestoreState(to *bitReader, from *bitReaderState) {
  75. to.val_ = from.val_
  76. to.bit_pos_ = from.bit_pos_
  77. to.input = from.input
  78. to.input_len = from.input_len
  79. to.byte_pos = from.byte_pos
  80. }
  81. func getAvailableBits(br *bitReader) uint32 {
  82. return 64 - br.bit_pos_
  83. }
  84. /* Returns amount of unread bytes the bit reader still has buffered from the
  85. BrotliInput, including whole bytes in br->val_. */
  86. func getRemainingBytes(br *bitReader) uint {
  87. return uint(uint32(br.input_len-br.byte_pos) + (getAvailableBits(br) >> 3))
  88. }
  89. /* Checks if there is at least |num| bytes left in the input ring-buffer
  90. (excluding the bits remaining in br->val_). */
  91. func checkInputAmount(br *bitReader, num uint) bool {
  92. return br.input_len-br.byte_pos >= num
  93. }
  94. /* Guarantees that there are at least |n_bits| + 1 bits in accumulator.
  95. Precondition: accumulator contains at least 1 bit.
  96. |n_bits| should be in the range [1..24] for regular build. For portable
  97. non-64-bit little-endian build only 16 bits are safe to request. */
  98. func fillBitWindow(br *bitReader, n_bits uint32) {
  99. if br.bit_pos_ >= 32 {
  100. br.val_ >>= 32
  101. br.bit_pos_ ^= 32 /* here same as -= 32 because of the if condition */
  102. br.val_ |= (uint64(binary.LittleEndian.Uint32(br.input[br.byte_pos:]))) << 32
  103. br.byte_pos += 4
  104. }
  105. }
  106. /* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no
  107. more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */
  108. func fillBitWindow16(br *bitReader) {
  109. fillBitWindow(br, 17)
  110. }
  111. /* Tries to pull one byte of input to accumulator.
  112. Returns false if there is no input available. */
  113. func pullByte(br *bitReader) bool {
  114. if br.byte_pos == br.input_len {
  115. return false
  116. }
  117. br.val_ >>= 8
  118. br.val_ |= (uint64(br.input[br.byte_pos])) << 56
  119. br.bit_pos_ -= 8
  120. br.byte_pos++
  121. return true
  122. }
  123. /* Returns currently available bits.
  124. The number of valid bits could be calculated by BrotliGetAvailableBits. */
  125. func getBitsUnmasked(br *bitReader) uint64 {
  126. return br.val_ >> br.bit_pos_
  127. }
  128. /* Like BrotliGetBits, but does not mask the result.
  129. The result contains at least 16 valid bits. */
  130. func get16BitsUnmasked(br *bitReader) uint32 {
  131. fillBitWindow(br, 16)
  132. return uint32(getBitsUnmasked(br))
  133. }
  134. /* Returns the specified number of bits from |br| without advancing bit
  135. position. */
  136. func getBits(br *bitReader, n_bits uint32) uint32 {
  137. fillBitWindow(br, n_bits)
  138. return uint32(getBitsUnmasked(br)) & bitMask(n_bits)
  139. }
  140. /* Tries to peek the specified amount of bits. Returns false, if there
  141. is not enough input. */
  142. func safeGetBits(br *bitReader, n_bits uint32, val *uint32) bool {
  143. for getAvailableBits(br) < n_bits {
  144. if !pullByte(br) {
  145. return false
  146. }
  147. }
  148. *val = uint32(getBitsUnmasked(br)) & bitMask(n_bits)
  149. return true
  150. }
  151. /* Advances the bit pos by |n_bits|. */
  152. func dropBits(br *bitReader, n_bits uint32) {
  153. br.bit_pos_ += n_bits
  154. }
  155. func bitReaderUnload(br *bitReader) {
  156. var unused_bytes uint32 = getAvailableBits(br) >> 3
  157. var unused_bits uint32 = unused_bytes << 3
  158. br.byte_pos -= uint(unused_bytes)
  159. if unused_bits == 64 {
  160. br.val_ = 0
  161. } else {
  162. br.val_ <<= unused_bits
  163. }
  164. br.bit_pos_ += unused_bits
  165. }
  166. /* Reads the specified number of bits from |br| and advances the bit pos.
  167. Precondition: accumulator MUST contain at least |n_bits|. */
  168. func takeBits(br *bitReader, n_bits uint32, val *uint32) {
  169. *val = uint32(getBitsUnmasked(br)) & bitMask(n_bits)
  170. dropBits(br, n_bits)
  171. }
  172. /* Reads the specified number of bits from |br| and advances the bit pos.
  173. Assumes that there is enough input to perform BrotliFillBitWindow. */
  174. func readBits(br *bitReader, n_bits uint32) uint32 {
  175. var val uint32
  176. fillBitWindow(br, n_bits)
  177. takeBits(br, n_bits, &val)
  178. return val
  179. }
  180. /* Tries to read the specified amount of bits. Returns false, if there
  181. is not enough input. |n_bits| MUST be positive. */
  182. func safeReadBits(br *bitReader, n_bits uint32, val *uint32) bool {
  183. for getAvailableBits(br) < n_bits {
  184. if !pullByte(br) {
  185. return false
  186. }
  187. }
  188. takeBits(br, n_bits, val)
  189. return true
  190. }
  191. /* Advances the bit reader position to the next byte boundary and verifies
  192. that any skipped bits are set to zero. */
  193. func bitReaderJumpToByteBoundary(br *bitReader) bool {
  194. var pad_bits_count uint32 = getAvailableBits(br) & 0x7
  195. var pad_bits uint32 = 0
  196. if pad_bits_count != 0 {
  197. takeBits(br, pad_bits_count, &pad_bits)
  198. }
  199. return pad_bits == 0
  200. }
  201. /* Copies remaining input bytes stored in the bit reader to the output. Value
  202. |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be
  203. warmed up again after this. */
  204. func copyBytes(dest []byte, br *bitReader, num uint) {
  205. for getAvailableBits(br) >= 8 && num > 0 {
  206. dest[0] = byte(getBitsUnmasked(br))
  207. dropBits(br, 8)
  208. dest = dest[1:]
  209. num--
  210. }
  211. copy(dest, br.input[br.byte_pos:][:num])
  212. br.byte_pos += num
  213. }
  214. func initBitReader(br *bitReader) {
  215. br.val_ = 0
  216. br.bit_pos_ = 64
  217. }
  218. func warmupBitReader(br *bitReader) bool {
  219. /* Fixing alignment after unaligned BrotliFillWindow would result accumulator
  220. overflow. If unalignment is caused by BrotliSafeReadBits, then there is
  221. enough space in accumulator to fix alignment. */
  222. if getAvailableBits(br) == 0 {
  223. if !pullByte(br) {
  224. return false
  225. }
  226. }
  227. return true
  228. }