h10.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package brotli
  2. import "encoding/binary"
  3. /* Copyright 2016 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. func (*h10) HashTypeLength() uint {
  8. return 4
  9. }
  10. func (*h10) StoreLookahead() uint {
  11. return 128
  12. }
  13. func hashBytesH10(data []byte) uint32 {
  14. var h uint32 = binary.LittleEndian.Uint32(data) * kHashMul32
  15. /* The higher bits contain more mixture from the multiplication,
  16. so we take our results from there. */
  17. return h >> (32 - 17)
  18. }
  19. /* A (forgetful) hash table where each hash bucket contains a binary tree of
  20. sequences whose first 4 bytes share the same hash code.
  21. Each sequence is 128 long and is identified by its starting
  22. position in the input data. The binary tree is sorted by the lexicographic
  23. order of the sequences, and it is also a max-heap with respect to the
  24. starting positions. */
  25. type h10 struct {
  26. hasherCommon
  27. window_mask_ uint
  28. buckets_ [1 << 17]uint32
  29. invalid_pos_ uint32
  30. forest []uint32
  31. }
  32. func (h *h10) Initialize(params *encoderParams) {
  33. h.window_mask_ = (1 << params.lgwin) - 1
  34. h.invalid_pos_ = uint32(0 - h.window_mask_)
  35. var num_nodes uint = uint(1) << params.lgwin
  36. h.forest = make([]uint32, 2*num_nodes)
  37. }
  38. func (h *h10) Prepare(one_shot bool, input_size uint, data []byte) {
  39. var invalid_pos uint32 = h.invalid_pos_
  40. var i uint32
  41. for i = 0; i < 1<<17; i++ {
  42. h.buckets_[i] = invalid_pos
  43. }
  44. }
  45. func leftChildIndexH10(self *h10, pos uint) uint {
  46. return 2 * (pos & self.window_mask_)
  47. }
  48. func rightChildIndexH10(self *h10, pos uint) uint {
  49. return 2*(pos&self.window_mask_) + 1
  50. }
  51. /* Stores the hash of the next 4 bytes and in a single tree-traversal, the
  52. hash bucket's binary tree is searched for matches and is re-rooted at the
  53. current position.
  54. If less than 128 data is available, the hash bucket of the
  55. current position is searched for matches, but the state of the hash table
  56. is not changed, since we can not know the final sorting order of the
  57. current (incomplete) sequence.
  58. This function must be called with increasing cur_ix positions. */
  59. func storeAndFindMatchesH10(self *h10, data []byte, cur_ix uint, ring_buffer_mask uint, max_length uint, max_backward uint, best_len *uint, matches []backwardMatch) []backwardMatch {
  60. var cur_ix_masked uint = cur_ix & ring_buffer_mask
  61. var max_comp_len uint = brotli_min_size_t(max_length, 128)
  62. var should_reroot_tree bool = (max_length >= 128)
  63. var key uint32 = hashBytesH10(data[cur_ix_masked:])
  64. var forest []uint32 = self.forest
  65. var prev_ix uint = uint(self.buckets_[key])
  66. var node_left uint = leftChildIndexH10(self, cur_ix)
  67. var node_right uint = rightChildIndexH10(self, cur_ix)
  68. var best_len_left uint = 0
  69. var best_len_right uint = 0
  70. var depth_remaining uint
  71. /* The forest index of the rightmost node of the left subtree of the new
  72. root, updated as we traverse and re-root the tree of the hash bucket. */
  73. /* The forest index of the leftmost node of the right subtree of the new
  74. root, updated as we traverse and re-root the tree of the hash bucket. */
  75. /* The match length of the rightmost node of the left subtree of the new
  76. root, updated as we traverse and re-root the tree of the hash bucket. */
  77. /* The match length of the leftmost node of the right subtree of the new
  78. root, updated as we traverse and re-root the tree of the hash bucket. */
  79. if should_reroot_tree {
  80. self.buckets_[key] = uint32(cur_ix)
  81. }
  82. for depth_remaining = 64; ; depth_remaining-- {
  83. var backward uint = cur_ix - prev_ix
  84. var prev_ix_masked uint = prev_ix & ring_buffer_mask
  85. if backward == 0 || backward > max_backward || depth_remaining == 0 {
  86. if should_reroot_tree {
  87. forest[node_left] = self.invalid_pos_
  88. forest[node_right] = self.invalid_pos_
  89. }
  90. break
  91. }
  92. {
  93. var cur_len uint = brotli_min_size_t(best_len_left, best_len_right)
  94. var len uint
  95. assert(cur_len <= 128)
  96. len = cur_len + findMatchLengthWithLimit(data[cur_ix_masked+cur_len:], data[prev_ix_masked+cur_len:], max_length-cur_len)
  97. if matches != nil && len > *best_len {
  98. *best_len = uint(len)
  99. initBackwardMatch(&matches[0], backward, uint(len))
  100. matches = matches[1:]
  101. }
  102. if len >= max_comp_len {
  103. if should_reroot_tree {
  104. forest[node_left] = forest[leftChildIndexH10(self, prev_ix)]
  105. forest[node_right] = forest[rightChildIndexH10(self, prev_ix)]
  106. }
  107. break
  108. }
  109. if data[cur_ix_masked+len] > data[prev_ix_masked+len] {
  110. best_len_left = uint(len)
  111. if should_reroot_tree {
  112. forest[node_left] = uint32(prev_ix)
  113. }
  114. node_left = rightChildIndexH10(self, prev_ix)
  115. prev_ix = uint(forest[node_left])
  116. } else {
  117. best_len_right = uint(len)
  118. if should_reroot_tree {
  119. forest[node_right] = uint32(prev_ix)
  120. }
  121. node_right = leftChildIndexH10(self, prev_ix)
  122. prev_ix = uint(forest[node_right])
  123. }
  124. }
  125. }
  126. return matches
  127. }
  128. /* Finds all backward matches of &data[cur_ix & ring_buffer_mask] up to the
  129. length of max_length and stores the position cur_ix in the hash table.
  130. Sets *num_matches to the number of matches found, and stores the found
  131. matches in matches[0] to matches[*num_matches - 1]. The matches will be
  132. sorted by strictly increasing length and (non-strictly) increasing
  133. distance. */
  134. func findAllMatchesH10(handle *h10, dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, cur_ix uint, max_length uint, max_backward uint, gap uint, params *encoderParams, matches []backwardMatch) uint {
  135. var orig_matches []backwardMatch = matches
  136. var cur_ix_masked uint = cur_ix & ring_buffer_mask
  137. var best_len uint = 1
  138. var short_match_max_backward uint
  139. if params.quality != hqZopflificationQuality {
  140. short_match_max_backward = 16
  141. } else {
  142. short_match_max_backward = 64
  143. }
  144. var stop uint = cur_ix - short_match_max_backward
  145. var dict_matches [maxStaticDictionaryMatchLen + 1]uint32
  146. var i uint
  147. if cur_ix < short_match_max_backward {
  148. stop = 0
  149. }
  150. for i = cur_ix - 1; i > stop && best_len <= 2; i-- {
  151. var prev_ix uint = i
  152. var backward uint = cur_ix - prev_ix
  153. if backward > max_backward {
  154. break
  155. }
  156. prev_ix &= ring_buffer_mask
  157. if data[cur_ix_masked] != data[prev_ix] || data[cur_ix_masked+1] != data[prev_ix+1] {
  158. continue
  159. }
  160. {
  161. var len uint = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length)
  162. if len > best_len {
  163. best_len = uint(len)
  164. initBackwardMatch(&matches[0], backward, uint(len))
  165. matches = matches[1:]
  166. }
  167. }
  168. }
  169. if best_len < max_length {
  170. matches = storeAndFindMatchesH10(handle, data, cur_ix, ring_buffer_mask, max_length, max_backward, &best_len, matches)
  171. }
  172. for i = 0; i <= maxStaticDictionaryMatchLen; i++ {
  173. dict_matches[i] = kInvalidMatch
  174. }
  175. {
  176. var minlen uint = brotli_max_size_t(4, best_len+1)
  177. if findAllStaticDictionaryMatches(dictionary, data[cur_ix_masked:], minlen, max_length, dict_matches[0:]) {
  178. var maxlen uint = brotli_min_size_t(maxStaticDictionaryMatchLen, max_length)
  179. var l uint
  180. for l = minlen; l <= maxlen; l++ {
  181. var dict_id uint32 = dict_matches[l]
  182. if dict_id < kInvalidMatch {
  183. var distance uint = max_backward + gap + uint(dict_id>>5) + 1
  184. if distance <= params.dist.max_distance {
  185. initDictionaryBackwardMatch(&matches[0], distance, l, uint(dict_id&31))
  186. matches = matches[1:]
  187. }
  188. }
  189. }
  190. }
  191. }
  192. return uint(-cap(matches) + cap(orig_matches))
  193. }
  194. /* Stores the hash of the next 4 bytes and re-roots the binary tree at the
  195. current sequence, without returning any matches.
  196. REQUIRES: ix + 128 <= end-of-current-block */
  197. func (h *h10) Store(data []byte, mask uint, ix uint) {
  198. var max_backward uint = h.window_mask_ - windowGap + 1
  199. /* Maximum distance is window size - 16, see section 9.1. of the spec. */
  200. storeAndFindMatchesH10(h, data, ix, mask, 128, max_backward, nil, nil)
  201. }
  202. func (h *h10) StoreRange(data []byte, mask uint, ix_start uint, ix_end uint) {
  203. var i uint = ix_start
  204. var j uint = ix_start
  205. if ix_start+63 <= ix_end {
  206. i = ix_end - 63
  207. }
  208. if ix_start+512 <= i {
  209. for ; j < i; j += 8 {
  210. h.Store(data, mask, j)
  211. }
  212. }
  213. for ; i < ix_end; i++ {
  214. h.Store(data, mask, i)
  215. }
  216. }
  217. func (h *h10) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) {
  218. if num_bytes >= h.HashTypeLength()-1 && position >= 128 {
  219. var i_start uint = position - 128 + 1
  220. var i_end uint = brotli_min_size_t(position, i_start+num_bytes)
  221. /* Store the last `128 - 1` positions in the hasher.
  222. These could not be calculated before, since they require knowledge
  223. of both the previous and the current block. */
  224. var i uint
  225. for i = i_start; i < i_end; i++ {
  226. /* Maximum distance is window size - 16, see section 9.1. of the spec.
  227. Furthermore, we have to make sure that we don't look further back
  228. from the start of the next block than the window size, otherwise we
  229. could access already overwritten areas of the ring-buffer. */
  230. var max_backward uint = h.window_mask_ - brotli_max_size_t(windowGap-1, position-i)
  231. /* We know that i + 128 <= position + num_bytes, i.e. the
  232. end of the current block and that we have at least
  233. 128 tail in the ring-buffer. */
  234. storeAndFindMatchesH10(h, ringbuffer, i, ringbuffer_mask, 128, max_backward, nil, nil)
  235. }
  236. }
  237. }
  238. /* MAX_NUM_MATCHES == 64 + MAX_TREE_SEARCH_DEPTH */
  239. const maxNumMatchesH10 = 128
  240. func (*h10) FindLongestMatch(dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *hasherSearchResult) {
  241. panic("unimplemented")
  242. }
  243. func (*h10) PrepareDistanceCache(distance_cache []int) {
  244. panic("unimplemented")
  245. }