blob: 0e8b1630c04a5680d419bf0dbb1dfc2699a3960f [file] [log] [blame]
Abhay Kumara2ae5992025-11-10 14:02:24 +00001// Copyright 2011 The Snappy-Go Authors. All rights reserved.
2// Modified for deflate by Klaus Post (c) 2015.
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file.
5
6package flate
7
8import (
9 "fmt"
10 "math/bits"
11
12 "github.com/klauspost/compress/internal/le"
13)
14
15type fastEnc interface {
16 Encode(dst *tokens, src []byte)
17 Reset()
18}
19
20func newFastEnc(level int) fastEnc {
21 switch level {
22 case 1:
23 return &fastEncL1{fastGen: fastGen{cur: maxStoreBlockSize}}
24 case 2:
25 return &fastEncL2{fastGen: fastGen{cur: maxStoreBlockSize}}
26 case 3:
27 return &fastEncL3{fastGen: fastGen{cur: maxStoreBlockSize}}
28 case 4:
29 return &fastEncL4{fastGen: fastGen{cur: maxStoreBlockSize}}
30 case 5:
31 return &fastEncL5{fastGen: fastGen{cur: maxStoreBlockSize}}
32 case 6:
33 return &fastEncL6{fastGen: fastGen{cur: maxStoreBlockSize}}
34 default:
35 panic("invalid level specified")
36 }
37}
38
39const (
40 tableBits = 15 // Bits used in the table
41 tableSize = 1 << tableBits // Size of the table
42 tableShift = 32 - tableBits // Right-shift to get the tableBits most significant bits of a uint32.
43 baseMatchOffset = 1 // The smallest match offset
44 baseMatchLength = 3 // The smallest match length per the RFC section 3.2.5
45 maxMatchOffset = 1 << 15 // The largest match offset
46
47 bTableBits = 17 // Bits used in the big tables
48 bTableSize = 1 << bTableBits // Size of the table
49 allocHistory = maxStoreBlockSize * 5 // Size to preallocate for history.
50 bufferReset = (1 << 31) - allocHistory - maxStoreBlockSize - 1 // Reset the buffer offset when reaching this.
51)
52
53const (
54 prime3bytes = 506832829
55 prime4bytes = 2654435761
56 prime5bytes = 889523592379
57 prime6bytes = 227718039650203
58 prime7bytes = 58295818150454627
59 prime8bytes = 0xcf1bbcdcb7a56463
60)
61
62func load3232(b []byte, i int32) uint32 {
63 return le.Load32(b, i)
64}
65
66func load6432(b []byte, i int32) uint64 {
67 return le.Load64(b, i)
68}
69
70type tableEntry struct {
71 offset int32
72}
73
74// fastGen maintains the table for matches,
75// and the previous byte block for level 2.
76// This is the generic implementation.
77type fastGen struct {
78 hist []byte
79 cur int32
80}
81
82func (e *fastGen) addBlock(src []byte) int32 {
83 // check if we have space already
84 if len(e.hist)+len(src) > cap(e.hist) {
85 if cap(e.hist) == 0 {
86 e.hist = make([]byte, 0, allocHistory)
87 } else {
88 if cap(e.hist) < maxMatchOffset*2 {
89 panic("unexpected buffer size")
90 }
91 // Move down
92 offset := int32(len(e.hist)) - maxMatchOffset
93 // copy(e.hist[0:maxMatchOffset], e.hist[offset:])
94 *(*[maxMatchOffset]byte)(e.hist) = *(*[maxMatchOffset]byte)(e.hist[offset:])
95 e.cur += offset
96 e.hist = e.hist[:maxMatchOffset]
97 }
98 }
99 s := int32(len(e.hist))
100 e.hist = append(e.hist, src...)
101 return s
102}
103
104type tableEntryPrev struct {
105 Cur tableEntry
106 Prev tableEntry
107}
108
109// hash7 returns the hash of the lowest 7 bytes of u to fit in a hash table with h bits.
110// Preferably h should be a constant and should always be <64.
111func hash7(u uint64, h uint8) uint32 {
112 return uint32(((u << (64 - 56)) * prime7bytes) >> ((64 - h) & reg8SizeMask64))
113}
114
115// hashLen returns a hash of the lowest mls bytes of with length output bits.
116// mls must be >=3 and <=8. Any other value will return hash for 4 bytes.
117// length should always be < 32.
118// Preferably length and mls should be a constant for inlining.
119func hashLen(u uint64, length, mls uint8) uint32 {
120 switch mls {
121 case 3:
122 return (uint32(u<<8) * prime3bytes) >> (32 - length)
123 case 5:
124 return uint32(((u << (64 - 40)) * prime5bytes) >> (64 - length))
125 case 6:
126 return uint32(((u << (64 - 48)) * prime6bytes) >> (64 - length))
127 case 7:
128 return uint32(((u << (64 - 56)) * prime7bytes) >> (64 - length))
129 case 8:
130 return uint32((u * prime8bytes) >> (64 - length))
131 default:
132 return (uint32(u) * prime4bytes) >> (32 - length)
133 }
134}
135
136// matchlen will return the match length between offsets and t in src.
137// The maximum length returned is maxMatchLength - 4.
138// It is assumed that s > t, that t >=0 and s < len(src).
139func (e *fastGen) matchlen(s, t int, src []byte) int32 {
140 if debugDeflate {
141 if t >= s {
142 panic(fmt.Sprint("t >=s:", t, s))
143 }
144 if int(s) >= len(src) {
145 panic(fmt.Sprint("s >= len(src):", s, len(src)))
146 }
147 if t < 0 {
148 panic(fmt.Sprint("t < 0:", t))
149 }
150 if s-t > maxMatchOffset {
151 panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")"))
152 }
153 }
154 s1 := min(s+maxMatchLength-4, len(src))
155 left := s1 - s
156 n := int32(0)
157 for left >= 8 {
158 diff := le.Load64(src, s) ^ le.Load64(src, t)
159 if diff != 0 {
160 return n + int32(bits.TrailingZeros64(diff)>>3)
161 }
162 s += 8
163 t += 8
164 n += 8
165 left -= 8
166 }
167
168 a := src[s:s1]
169 b := src[t:]
170 for i := range a {
171 if a[i] != b[i] {
172 break
173 }
174 n++
175 }
176 return n
177}
178
179// matchlenLong will return the match length between offsets and t in src.
180// It is assumed that s > t, that t >=0 and s < len(src).
181func (e *fastGen) matchlenLong(s, t int, src []byte) int32 {
182 if debugDeflate {
183 if t >= s {
184 panic(fmt.Sprint("t >=s:", t, s))
185 }
186 if int(s) >= len(src) {
187 panic(fmt.Sprint("s >= len(src):", s, len(src)))
188 }
189 if t < 0 {
190 panic(fmt.Sprint("t < 0:", t))
191 }
192 if s-t > maxMatchOffset {
193 panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")"))
194 }
195 }
196 // Extend the match to be as long as possible.
197 left := len(src) - s
198 n := int32(0)
199 for left >= 8 {
200 diff := le.Load64(src, s) ^ le.Load64(src, t)
201 if diff != 0 {
202 return n + int32(bits.TrailingZeros64(diff)>>3)
203 }
204 s += 8
205 t += 8
206 n += 8
207 left -= 8
208 }
209
210 a := src[s:]
211 b := src[t:]
212 for i := range a {
213 if a[i] != b[i] {
214 break
215 }
216 n++
217 }
218 return n
219}
220
221// Reset the encoding table.
222func (e *fastGen) Reset() {
223 if cap(e.hist) < allocHistory {
224 e.hist = make([]byte, 0, allocHistory)
225 }
226 // We offset current position so everything will be out of reach.
227 // If we are above the buffer reset it will be cleared anyway since len(hist) == 0.
228 if e.cur <= bufferReset {
229 e.cur += maxMatchOffset + int32(len(e.hist))
230 }
231 e.hist = e.hist[:0]
232}