-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlz_bin_tree.go
355 lines (325 loc) · 8.53 KB
/
lz_bin_tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright (c) 2010, Andrei Vieru. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lzma
import "io"
const (
kHash2Size = 1 << 10
kHash3Size = 1 << 16
kBT2HashSize = 1 << 16
kStartMaxLen = 1
kHash3Offset = kHash2Size
kEmptyHashValue = 0
kMaxValForNormalize = (1 << 30) - 1
)
type lzBinTree struct {
iw *lzInWindow
son []uint32
hash []uint32
cyclicBufPos uint32
cyclicBufSize uint32
matchMaxLen uint32
cutValue uint32
hashMask uint32
hashSizeSum uint32
kvNumHashDirectBytes uint32
kvMinMatchCheck uint32
kvFixHashSize uint32
hashArray bool
}
func newLzBinTree(r io.Reader, historySize, keepAddBufBefore, matchMaxLen, keepAddBufAfter, numHashBytes uint32) *lzBinTree {
bt := &lzBinTree{
son: make([]uint32, (historySize+1)*2), // history size is the dictSize from the encoder
cyclicBufPos: 0,
cyclicBufSize: historySize + 1,
matchMaxLen: matchMaxLen,
cutValue: 16 + (matchMaxLen >> 1),
}
winSizeReserv := (historySize+keepAddBufBefore+matchMaxLen+keepAddBufAfter)/2 + 256
bt.iw = newLzInWindow(r, historySize+keepAddBufBefore, matchMaxLen+keepAddBufAfter, winSizeReserv)
if numHashBytes > 2 {
bt.hashArray = true
bt.kvNumHashDirectBytes = 0
bt.kvMinMatchCheck = 4
bt.kvFixHashSize = kHash2Size + kHash3Size
} else {
bt.hashArray = false
bt.kvNumHashDirectBytes = 2
bt.kvMinMatchCheck = 3
bt.kvFixHashSize = 0
}
hs := uint32(kBT2HashSize)
if bt.hashArray == true {
hs = historySize - 1
hs |= hs >> 1
hs |= hs >> 2
hs |= hs >> 4
hs |= hs >> 8
hs >>= 1
hs |= 0xFFFF
if hs > 1<<24 {
hs >>= 1
}
bt.hashMask = hs
hs++
hs += bt.kvFixHashSize
}
bt.hashSizeSum = hs
bt.hash = make([]uint32, bt.hashSizeSum)
for i := uint32(0); i < bt.hashSizeSum; i++ {
bt.hash[i] = kEmptyHashValue
}
bt.iw.reduceOffsets(0xFFFFFFFF)
return bt
}
func normalizeLinks(items []uint32, numItems, subValue uint32) {
for i := uint32(0); i < numItems; i++ {
value := items[i]
if value <= subValue {
value = kEmptyHashValue
} else {
value -= subValue
}
items[i] = value
}
}
func (bt *lzBinTree) normalize() {
subValue := bt.iw.pos - bt.cyclicBufSize
normalizeLinks(bt.son, bt.cyclicBufSize*2, subValue)
normalizeLinks(bt.hash, bt.hashSizeSum, subValue)
bt.iw.reduceOffsets(subValue)
}
func (bt *lzBinTree) movePos() {
bt.cyclicBufPos++
if bt.cyclicBufPos >= bt.cyclicBufSize {
bt.cyclicBufPos = 0
}
bt.iw.movePos()
if bt.iw.pos == kMaxValForNormalize {
bt.normalize()
}
}
func (bt *lzBinTree) getMatches(distances []uint32) uint32 {
var lenLimit uint32
if bt.iw.pos+bt.matchMaxLen <= bt.iw.streamPos {
lenLimit = bt.matchMaxLen
} else {
lenLimit = bt.iw.streamPos - bt.iw.pos
if lenLimit < bt.kvMinMatchCheck {
bt.movePos()
return 0
}
}
offset := uint32(0)
matchMinPos := uint32(0)
if bt.iw.pos > bt.cyclicBufSize {
matchMinPos = bt.iw.pos - bt.cyclicBufSize
}
cur := bt.iw.bufOffset + bt.iw.pos
maxLen := uint32(kStartMaxLen)
var hashValue uint32
hash2Value := uint32(0)
hash3Value := uint32(0)
if bt.hashArray == true {
tmp := crcTable[bt.iw.buf[cur]] ^ uint32(bt.iw.buf[cur+1])
hash2Value = tmp & (kHash2Size - 1)
tmp ^= uint32(bt.iw.buf[cur+2]) << 8
hash3Value = tmp & (kHash3Size - 1)
hashValue = (tmp ^ crcTable[bt.iw.buf[cur+3]]<<5) & bt.hashMask
} else {
hashValue = uint32(bt.iw.buf[cur]) ^ uint32(bt.iw.buf[cur+1])<<8
}
curMatch := bt.hash[bt.kvFixHashSize+hashValue]
if bt.hashArray == true {
curMatch2 := bt.hash[hash2Value]
curMatch3 := bt.hash[kHash3Offset+hash3Value]
bt.hash[hash2Value] = bt.iw.pos
bt.hash[kHash3Offset+hash3Value] = bt.iw.pos
if curMatch2 > matchMinPos {
if bt.iw.buf[bt.iw.bufOffset+curMatch2] == bt.iw.buf[cur] {
maxLen = 2
distances[offset] = maxLen
offset++
distances[offset] = bt.iw.pos - curMatch2 - 1
offset++
}
}
if curMatch3 > matchMinPos {
if bt.iw.buf[bt.iw.bufOffset+curMatch3] == bt.iw.buf[cur] {
if curMatch3 == curMatch2 {
offset -= 2
}
maxLen = 3
distances[offset] = maxLen
offset++
distances[offset] = bt.iw.pos - curMatch3 - 1
offset++
curMatch2 = curMatch3
}
}
if offset != 0 && curMatch2 == curMatch {
offset -= 2
maxLen = kStartMaxLen
}
}
bt.hash[bt.kvFixHashSize+hashValue] = bt.iw.pos
if bt.kvNumHashDirectBytes != 0 {
if curMatch > matchMinPos {
if bt.iw.buf[bt.iw.bufOffset+curMatch+bt.kvNumHashDirectBytes] != bt.iw.buf[cur+bt.kvNumHashDirectBytes] {
maxLen = bt.kvNumHashDirectBytes
distances[offset] = maxLen
offset++
distances[offset] = bt.iw.pos - curMatch - 1
offset++
}
}
}
ptr0 := bt.cyclicBufPos<<1 + 1
ptr1 := bt.cyclicBufPos << 1
len0 := bt.kvNumHashDirectBytes
len1 := bt.kvNumHashDirectBytes
count := bt.cutValue
for {
if curMatch <= matchMinPos || count == 0 {
bt.son[ptr1] = kEmptyHashValue
bt.son[ptr0] = kEmptyHashValue
break
}
count--
delta := bt.iw.pos - curMatch
var cyclicPos uint32
if delta <= bt.cyclicBufPos {
cyclicPos = (bt.cyclicBufPos - delta) << 1
} else {
cyclicPos = (bt.cyclicBufPos - delta + bt.cyclicBufSize) << 1
}
pby1 := bt.iw.bufOffset + curMatch
length := minUInt32(len0, len1)
if bt.iw.buf[pby1+length] == bt.iw.buf[cur+length] {
for length++; length != lenLimit; length++ {
if bt.iw.buf[pby1+length] != bt.iw.buf[cur+length] {
break
}
}
if maxLen < length {
maxLen = length
distances[offset] = maxLen
offset++
distances[offset] = delta - 1
offset++
if length == lenLimit {
bt.son[ptr1] = bt.son[cyclicPos]
bt.son[ptr0] = bt.son[cyclicPos+1]
break
}
}
}
if bt.iw.buf[pby1+length] < bt.iw.buf[cur+length] {
bt.son[ptr1] = curMatch
ptr1 = cyclicPos + 1
curMatch = bt.son[ptr1]
len1 = length
} else {
bt.son[ptr0] = curMatch
ptr0 = cyclicPos
curMatch = bt.son[ptr0]
len0 = length
}
}
bt.movePos()
return offset
}
func (bt *lzBinTree) skip(num uint32) {
for i := uint32(0); i < num; i++ {
var lenLimit uint32
if bt.iw.pos+bt.matchMaxLen <= bt.iw.streamPos {
lenLimit = bt.matchMaxLen
} else {
lenLimit = bt.iw.streamPos - bt.iw.pos
if lenLimit < bt.kvMinMatchCheck {
bt.movePos()
continue
}
}
matchMinPos := uint32(0)
if bt.iw.pos > bt.cyclicBufSize {
matchMinPos = bt.iw.pos - bt.cyclicBufSize
}
cur := bt.iw.bufOffset + bt.iw.pos
var hashValue uint32
if bt.hashArray == true {
tmp := crcTable[bt.iw.buf[cur]] ^ uint32(bt.iw.buf[cur+1])
hash2Value := tmp & (kHash2Size - 1)
bt.hash[hash2Value] = bt.iw.pos
tmp ^= uint32(bt.iw.buf[cur+2]) << 8
hash3Value := tmp & (kHash3Size - 1)
bt.hash[kHash3Offset+hash3Value] = bt.iw.pos
hashValue = (tmp ^ crcTable[bt.iw.buf[cur+3]]<<5) & bt.hashMask
} else {
hashValue = uint32(bt.iw.buf[cur]) ^ uint32(bt.iw.buf[cur+1])<<8
}
curMatch := bt.hash[bt.kvFixHashSize+hashValue]
bt.hash[bt.kvFixHashSize+hashValue] = bt.iw.pos
ptr0 := bt.cyclicBufPos<<1 + 1
ptr1 := bt.cyclicBufPos << 1
len0 := bt.kvNumHashDirectBytes
len1 := bt.kvNumHashDirectBytes
count := bt.cutValue
for {
if curMatch <= matchMinPos || count == 0 {
bt.son[ptr1] = kEmptyHashValue
bt.son[ptr0] = kEmptyHashValue
break
}
count--
delta := bt.iw.pos - curMatch
var cyclicPos uint32
if delta <= bt.cyclicBufPos {
cyclicPos = (bt.cyclicBufPos - delta) << 1
} else {
cyclicPos = (bt.cyclicBufPos - delta + bt.cyclicBufSize) << 1
}
pby1 := bt.iw.bufOffset + curMatch
length := minUInt32(len0, len1)
if bt.iw.buf[pby1+length] == bt.iw.buf[cur+length] {
for length++; length != lenLimit; length++ {
if bt.iw.buf[pby1+length] != bt.iw.buf[cur+length] {
break
}
}
if length == lenLimit {
bt.son[ptr1] = bt.son[cyclicPos]
bt.son[ptr0] = bt.son[cyclicPos+1]
break
}
}
if bt.iw.buf[pby1+length] < bt.iw.buf[cur+length] {
bt.son[ptr1] = curMatch
ptr1 = cyclicPos + 1
curMatch = bt.son[ptr1]
len1 = length
} else {
bt.son[ptr0] = curMatch
ptr0 = cyclicPos
curMatch = bt.son[ptr0]
len0 = length
}
}
bt.movePos()
}
}
var crcTable []uint32 = make([]uint32, 256)
// should be called in the encoder's contructor
func initCrcTable() {
for i := uint32(0); i < 256; i++ {
r := i
for j := 0; j < 8; j++ {
if r&1 != 0 {
r = r>>1 ^ 0xEDB88320
} else {
r >>= 1
}
}
crcTable[i] = r
}
}