7
"github.com/klauspost/compress/zstd/internal/xxhash"
15
// cur is the offset at the start of hist
17
// maximum offset. Should be at least 2x block size.
27
// CRC returns the underlying CRC writer.
28
func (e *fastBase) CRC() *xxhash.Digest {
32
// AppendCRC will append the CRC to the destination slice and return it.
33
func (e *fastBase) AppendCRC(dst []byte) []byte {
34
crc := e.crc.Sum(e.tmp[:0])
35
dst = append(dst, crc[7], crc[6], crc[5], crc[4])
39
// WindowSize returns the window size of the encoder,
40
// or a window size small enough to contain the input size, if > 0.
41
func (e *fastBase) WindowSize(size int64) int32 {
42
if size > 0 && size < int64(e.maxMatchOff) {
43
b := int32(1) << uint(bits.Len(uint(size)))
44
// Keep minimum window.
53
// Block returns the current block.
54
func (e *fastBase) Block() *blockEnc {
58
func (e *fastBase) addBlock(src []byte) int32 {
59
if debugAsserts && e.cur > bufferReset {
60
panic(fmt.Sprintf("ecur (%d) > buffer reset (%d)", e.cur, bufferReset))
62
// check if we have space already
63
if len(e.hist)+len(src) > cap(e.hist) {
65
e.ensureHist(len(src))
67
if cap(e.hist) < int(e.maxMatchOff+maxCompressedBlockSize) {
68
panic(fmt.Errorf("unexpected buffer cap %d, want at least %d with window %d", cap(e.hist), e.maxMatchOff+maxCompressedBlockSize, e.maxMatchOff))
71
offset := int32(len(e.hist)) - e.maxMatchOff
72
copy(e.hist[0:e.maxMatchOff], e.hist[offset:])
74
e.hist = e.hist[:e.maxMatchOff]
77
s := int32(len(e.hist))
78
e.hist = append(e.hist, src...)
82
// ensureHist will ensure that history can keep at least this many bytes.
83
func (e *fastBase) ensureHist(n int) {
88
if (e.lowMem && e.maxMatchOff > maxCompressedBlockSize) || e.maxMatchOff <= maxCompressedBlockSize {
89
l += maxCompressedBlockSize
93
// Make it at least 1MB.
94
if l < 1<<20 && !e.lowMem {
97
// Make it at least the requested size.
101
e.hist = make([]byte, 0, l)
104
// useBlock will replace the block with the provided one,
105
// but transfer recent offsets from the previous.
106
func (e *fastBase) UseBlock(enc *blockEnc) {
111
func (e *fastBase) matchlen(s, t int32, src []byte) int32 {
114
err := fmt.Sprintf("s (%d) < 0", s)
118
err := fmt.Sprintf("s (%d) < 0", s)
121
if s-t > e.maxMatchOff {
122
err := fmt.Sprintf("s (%d) - t (%d) > maxMatchOff (%d)", s, t, e.maxMatchOff)
125
if len(src)-int(s) > maxCompressedBlockSize {
126
panic(fmt.Sprintf("len(src)-s (%d) > maxCompressedBlockSize (%d)", len(src)-int(s), maxCompressedBlockSize))
132
end := int32((len(a) >> 3) << 3)
133
for i := int32(0); i < end; i += 8 {
134
if diff := load6432(a, i) ^ load6432(b, i); diff != 0 {
135
return i + int32(bits.TrailingZeros64(diff)>>3)
143
return int32(i) + end
146
return int32(len(a)) + end
149
// Reset the encoding table.
150
func (e *fastBase) resetBase(d *dict, singleBlock bool) {
152
e.blk = &blockEnc{lowMem: e.lowMem}
157
e.blk.initNewEncode()
168
e.ensureHist(d.DictContentSize() + maxCompressedBlockSize)
172
// We offset current position so everything will be out of reach.
173
// If above reset line, history will be purged.
174
if e.cur < bufferReset {
175
e.cur += e.maxMatchOff + int32(len(e.hist))
179
// Set offsets (currently not used)
180
for i, off := range d.offsets {
181
e.blk.recentOffsets[i] = uint32(off)
182
e.blk.prevRecentOffsets[i] = e.blk.recentOffsets[i]
185
e.blk.dictLitEnc = d.litEnc
186
e.hist = append(e.hist, d.content...)