1 | // 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 |
|
---|
6 | package flate
|
---|
7 |
|
---|
8 | import (
|
---|
9 | "fmt"
|
---|
10 | "math/bits"
|
---|
11 | )
|
---|
12 |
|
---|
13 | type fastEnc interface {
|
---|
14 | Encode(dst *tokens, src []byte)
|
---|
15 | Reset()
|
---|
16 | }
|
---|
17 |
|
---|
18 | func newFastEnc(level int) fastEnc {
|
---|
19 | switch level {
|
---|
20 | case 1:
|
---|
21 | return &fastEncL1{fastGen: fastGen{cur: maxStoreBlockSize}}
|
---|
22 | case 2:
|
---|
23 | return &fastEncL2{fastGen: fastGen{cur: maxStoreBlockSize}}
|
---|
24 | case 3:
|
---|
25 | return &fastEncL3{fastGen: fastGen{cur: maxStoreBlockSize}}
|
---|
26 | case 4:
|
---|
27 | return &fastEncL4{fastGen: fastGen{cur: maxStoreBlockSize}}
|
---|
28 | case 5:
|
---|
29 | return &fastEncL5{fastGen: fastGen{cur: maxStoreBlockSize}}
|
---|
30 | case 6:
|
---|
31 | return &fastEncL6{fastGen: fastGen{cur: maxStoreBlockSize}}
|
---|
32 | default:
|
---|
33 | panic("invalid level specified")
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | const (
|
---|
38 | tableBits = 15 // Bits used in the table
|
---|
39 | tableSize = 1 << tableBits // Size of the table
|
---|
40 | tableShift = 32 - tableBits // Right-shift to get the tableBits most significant bits of a uint32.
|
---|
41 | baseMatchOffset = 1 // The smallest match offset
|
---|
42 | baseMatchLength = 3 // The smallest match length per the RFC section 3.2.5
|
---|
43 | maxMatchOffset = 1 << 15 // The largest match offset
|
---|
44 |
|
---|
45 | bTableBits = 17 // Bits used in the big tables
|
---|
46 | bTableSize = 1 << bTableBits // Size of the table
|
---|
47 | allocHistory = maxStoreBlockSize * 10 // Size to preallocate for history.
|
---|
48 | bufferReset = (1 << 31) - allocHistory - maxStoreBlockSize - 1 // Reset the buffer offset when reaching this.
|
---|
49 | )
|
---|
50 |
|
---|
51 | const (
|
---|
52 | prime3bytes = 506832829
|
---|
53 | prime4bytes = 2654435761
|
---|
54 | prime5bytes = 889523592379
|
---|
55 | prime6bytes = 227718039650203
|
---|
56 | prime7bytes = 58295818150454627
|
---|
57 | prime8bytes = 0xcf1bbcdcb7a56463
|
---|
58 | )
|
---|
59 |
|
---|
60 | func load32(b []byte, i int) uint32 {
|
---|
61 | // Help the compiler eliminate bounds checks on the read so it can be done in a single read.
|
---|
62 | b = b[i:]
|
---|
63 | b = b[:4]
|
---|
64 | return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
|
---|
65 | }
|
---|
66 |
|
---|
67 | func load64(b []byte, i int) uint64 {
|
---|
68 | // Help the compiler eliminate bounds checks on the read so it can be done in a single read.
|
---|
69 | b = b[i:]
|
---|
70 | b = b[:8]
|
---|
71 | return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
|
---|
72 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
|
---|
73 | }
|
---|
74 |
|
---|
75 | func load3232(b []byte, i int32) uint32 {
|
---|
76 | // Help the compiler eliminate bounds checks on the read so it can be done in a single read.
|
---|
77 | b = b[i:]
|
---|
78 | b = b[:4]
|
---|
79 | return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
|
---|
80 | }
|
---|
81 |
|
---|
82 | func load6432(b []byte, i int32) uint64 {
|
---|
83 | // Help the compiler eliminate bounds checks on the read so it can be done in a single read.
|
---|
84 | b = b[i:]
|
---|
85 | b = b[:8]
|
---|
86 | return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
|
---|
87 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
|
---|
88 | }
|
---|
89 |
|
---|
90 | func hash(u uint32) uint32 {
|
---|
91 | return (u * 0x1e35a7bd) >> tableShift
|
---|
92 | }
|
---|
93 |
|
---|
94 | type tableEntry struct {
|
---|
95 | offset int32
|
---|
96 | }
|
---|
97 |
|
---|
98 | // fastGen maintains the table for matches,
|
---|
99 | // and the previous byte block for level 2.
|
---|
100 | // This is the generic implementation.
|
---|
101 | type fastGen struct {
|
---|
102 | hist []byte
|
---|
103 | cur int32
|
---|
104 | }
|
---|
105 |
|
---|
106 | func (e *fastGen) addBlock(src []byte) int32 {
|
---|
107 | // check if we have space already
|
---|
108 | if len(e.hist)+len(src) > cap(e.hist) {
|
---|
109 | if cap(e.hist) == 0 {
|
---|
110 | e.hist = make([]byte, 0, allocHistory)
|
---|
111 | } else {
|
---|
112 | if cap(e.hist) < maxMatchOffset*2 {
|
---|
113 | panic("unexpected buffer size")
|
---|
114 | }
|
---|
115 | // Move down
|
---|
116 | offset := int32(len(e.hist)) - maxMatchOffset
|
---|
117 | copy(e.hist[0:maxMatchOffset], e.hist[offset:])
|
---|
118 | e.cur += offset
|
---|
119 | e.hist = e.hist[:maxMatchOffset]
|
---|
120 | }
|
---|
121 | }
|
---|
122 | s := int32(len(e.hist))
|
---|
123 | e.hist = append(e.hist, src...)
|
---|
124 | return s
|
---|
125 | }
|
---|
126 |
|
---|
127 | // hash4 returns the hash of u to fit in a hash table with h bits.
|
---|
128 | // Preferably h should be a constant and should always be <32.
|
---|
129 | func hash4u(u uint32, h uint8) uint32 {
|
---|
130 | return (u * prime4bytes) >> ((32 - h) & 31)
|
---|
131 | }
|
---|
132 |
|
---|
133 | type tableEntryPrev struct {
|
---|
134 | Cur tableEntry
|
---|
135 | Prev tableEntry
|
---|
136 | }
|
---|
137 |
|
---|
138 | // hash4x64 returns the hash of the lowest 4 bytes of u to fit in a hash table with h bits.
|
---|
139 | // Preferably h should be a constant and should always be <32.
|
---|
140 | func hash4x64(u uint64, h uint8) uint32 {
|
---|
141 | return (uint32(u) * prime4bytes) >> ((32 - h) & 31)
|
---|
142 | }
|
---|
143 |
|
---|
144 | // hash7 returns the hash of the lowest 7 bytes of u to fit in a hash table with h bits.
|
---|
145 | // Preferably h should be a constant and should always be <64.
|
---|
146 | func hash7(u uint64, h uint8) uint32 {
|
---|
147 | return uint32(((u << (64 - 56)) * prime7bytes) >> ((64 - h) & 63))
|
---|
148 | }
|
---|
149 |
|
---|
150 | // hash8 returns the hash of u to fit in a hash table with h bits.
|
---|
151 | // Preferably h should be a constant and should always be <64.
|
---|
152 | func hash8(u uint64, h uint8) uint32 {
|
---|
153 | return uint32((u * prime8bytes) >> ((64 - h) & 63))
|
---|
154 | }
|
---|
155 |
|
---|
156 | // hash6 returns the hash of the lowest 6 bytes of u to fit in a hash table with h bits.
|
---|
157 | // Preferably h should be a constant and should always be <64.
|
---|
158 | func hash6(u uint64, h uint8) uint32 {
|
---|
159 | return uint32(((u << (64 - 48)) * prime6bytes) >> ((64 - h) & 63))
|
---|
160 | }
|
---|
161 |
|
---|
162 | // matchlen will return the match length between offsets and t in src.
|
---|
163 | // The maximum length returned is maxMatchLength - 4.
|
---|
164 | // It is assumed that s > t, that t >=0 and s < len(src).
|
---|
165 | func (e *fastGen) matchlen(s, t int32, src []byte) int32 {
|
---|
166 | if debugDecode {
|
---|
167 | if t >= s {
|
---|
168 | panic(fmt.Sprint("t >=s:", t, s))
|
---|
169 | }
|
---|
170 | if int(s) >= len(src) {
|
---|
171 | panic(fmt.Sprint("s >= len(src):", s, len(src)))
|
---|
172 | }
|
---|
173 | if t < 0 {
|
---|
174 | panic(fmt.Sprint("t < 0:", t))
|
---|
175 | }
|
---|
176 | if s-t > maxMatchOffset {
|
---|
177 | panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")"))
|
---|
178 | }
|
---|
179 | }
|
---|
180 | s1 := int(s) + maxMatchLength - 4
|
---|
181 | if s1 > len(src) {
|
---|
182 | s1 = len(src)
|
---|
183 | }
|
---|
184 |
|
---|
185 | // Extend the match to be as long as possible.
|
---|
186 | return int32(matchLen(src[s:s1], src[t:]))
|
---|
187 | }
|
---|
188 |
|
---|
189 | // matchlenLong will return the match length between offsets and t in src.
|
---|
190 | // It is assumed that s > t, that t >=0 and s < len(src).
|
---|
191 | func (e *fastGen) matchlenLong(s, t int32, src []byte) int32 {
|
---|
192 | if debugDecode {
|
---|
193 | if t >= s {
|
---|
194 | panic(fmt.Sprint("t >=s:", t, s))
|
---|
195 | }
|
---|
196 | if int(s) >= len(src) {
|
---|
197 | panic(fmt.Sprint("s >= len(src):", s, len(src)))
|
---|
198 | }
|
---|
199 | if t < 0 {
|
---|
200 | panic(fmt.Sprint("t < 0:", t))
|
---|
201 | }
|
---|
202 | if s-t > maxMatchOffset {
|
---|
203 | panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")"))
|
---|
204 | }
|
---|
205 | }
|
---|
206 | // Extend the match to be as long as possible.
|
---|
207 | return int32(matchLen(src[s:], src[t:]))
|
---|
208 | }
|
---|
209 |
|
---|
210 | // Reset the encoding table.
|
---|
211 | func (e *fastGen) Reset() {
|
---|
212 | if cap(e.hist) < allocHistory {
|
---|
213 | e.hist = make([]byte, 0, allocHistory)
|
---|
214 | }
|
---|
215 | // We offset current position so everything will be out of reach.
|
---|
216 | // If we are above the buffer reset it will be cleared anyway since len(hist) == 0.
|
---|
217 | if e.cur <= bufferReset {
|
---|
218 | e.cur += maxMatchOffset + int32(len(e.hist))
|
---|
219 | }
|
---|
220 | e.hist = e.hist[:0]
|
---|
221 | }
|
---|
222 |
|
---|
223 | // matchLen returns the maximum length.
|
---|
224 | // 'a' must be the shortest of the two.
|
---|
225 | func matchLen(a, b []byte) int {
|
---|
226 | b = b[:len(a)]
|
---|
227 | var checked int
|
---|
228 | if len(a) > 4 {
|
---|
229 | // Try 4 bytes first
|
---|
230 | if diff := load32(a, 0) ^ load32(b, 0); diff != 0 {
|
---|
231 | return bits.TrailingZeros32(diff) >> 3
|
---|
232 | }
|
---|
233 | // Switch to 8 byte matching.
|
---|
234 | checked = 4
|
---|
235 | a = a[4:]
|
---|
236 | b = b[4:]
|
---|
237 | for len(a) >= 8 {
|
---|
238 | b = b[:len(a)]
|
---|
239 | if diff := load64(a, 0) ^ load64(b, 0); diff != 0 {
|
---|
240 | return checked + (bits.TrailingZeros64(diff) >> 3)
|
---|
241 | }
|
---|
242 | checked += 8
|
---|
243 | a = a[8:]
|
---|
244 | b = b[8:]
|
---|
245 | }
|
---|
246 | }
|
---|
247 | b = b[:len(a)]
|
---|
248 | for i := range a {
|
---|
249 | if a[i] != b[i] {
|
---|
250 | return int(i) + checked
|
---|
251 | }
|
---|
252 | }
|
---|
253 | return len(a) + checked
|
---|
254 | }
|
---|