2023-12-29 03:09:32 +03:00
|
|
|
package brotli
|
|
|
|
|
2024-01-03 00:38:12 +03:00
|
|
|
import "github.com/andybalholm/brotli/matchfinder"
|
|
|
|
|
2023-12-29 03:09:32 +03:00
|
|
|
/* Copyright 2010 Google Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
Distributed under MIT license.
|
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Write bits into a byte array. */
|
|
|
|
|
|
|
|
type bitWriter struct {
|
|
|
|
dst []byte
|
|
|
|
|
|
|
|
// Data waiting to be written is the low nbits of bits.
|
|
|
|
bits uint64
|
|
|
|
nbits uint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bitWriter) writeBits(nb uint, b uint64) {
|
|
|
|
w.bits |= b << w.nbits
|
|
|
|
w.nbits += nb
|
|
|
|
if w.nbits >= 32 {
|
|
|
|
bits := w.bits
|
|
|
|
w.bits >>= 32
|
|
|
|
w.nbits -= 32
|
|
|
|
w.dst = append(w.dst,
|
|
|
|
byte(bits),
|
|
|
|
byte(bits>>8),
|
|
|
|
byte(bits>>16),
|
|
|
|
byte(bits>>24),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bitWriter) writeSingleBit(bit bool) {
|
|
|
|
if bit {
|
|
|
|
w.writeBits(1, 1)
|
|
|
|
} else {
|
|
|
|
w.writeBits(1, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bitWriter) jumpToByteBoundary() {
|
|
|
|
dst := w.dst
|
|
|
|
for w.nbits != 0 {
|
|
|
|
dst = append(dst, byte(w.bits))
|
|
|
|
w.bits >>= 8
|
|
|
|
if w.nbits > 8 { // Avoid underflow
|
|
|
|
w.nbits -= 8
|
|
|
|
} else {
|
|
|
|
w.nbits = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.bits = 0
|
|
|
|
w.dst = dst
|
|
|
|
}
|
2024-01-03 00:38:12 +03:00
|
|
|
|
|
|
|
func matchScore(m matchfinder.AbsoluteMatch) int {
|
|
|
|
return int(backwardReferenceScore(uint(m.End-m.Start), uint(m.Start-m.Match)))
|
|
|
|
}
|